位运算Pascal相关函数/过程

Function Getlen(P:longint):longint;
var
 i:longint;
  begin
  getlen:=0;
  for i:=1 to 31 do
    begin
    if p and 1=1 then getlen:=i;
    p:=p shr 1;
  end;
end;

Function GetBit(Num,bit:longint):boolean;
  begin
  if (num and (1 shl(bit-1)))=0 then exit(false) else exit(true);
end;

Procedure changeBit(var num:longint;bit:longint;opt:boolean);
  begin
  if opt then
    num:=num or (1 shl (bit-1))
  else
    num:=num and not (1 shl (bit-1));
end;

Function getblock(num:longint;start,len:longint):longint;
var
 i,tmp:longint;
  begin
  tmp:=0;
  for i:=1 to len do
    tmp:=tmp or (1 shl (i-1));
  Getblock:=(num and (tmp shl (start-1))) shr (start-1);
end;

Procedure changeBlock(var num:longint;start,opt:longint);
var
 len,tmp,i:longint;
  begin
  len:=getlen(opt);
  tmp:=0;
  for i:=1 to len do
    begin
    tmp:=tmp or 1;
    tmp:=tmp shl 1;
  end;
  tmp:=not (tmp shl (start-1));
  num:=num and tmp;
  num:=num or (opt shl (start-1));
end;      
原文地址:https://www.cnblogs.com/htfy/p/2412251.html