Functions for bit manipulation  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 23/01/00

Tip accessed 412 times

 


For those that also program microcontrollers, we sometimes miss some how many functions that they allow us to manipulate bits.
Here you have some how many that will allow you the basic thing:

  • To know if a bit is or not to one (EstaBitAUno)
  • To put to one a bit (PonBitAUno)
  • To put to zero a bit (PonBitACero)
  • To complement (to deny) the state of a bit (CambiaBit)



       function EstaBitAUno(const Numero: longint; const Bit: byte): boolean;
       begin
         result := (Numero and (1 shl Bit)) <> 0;
       end;
    
       function PonBitAUno(const Numero: longint; const Bit: byte): LongInt;
       begin
         result := Numero or (1 shl Bit);
       end;
    
       function PonBitBitaCero(const Numero: longint; const Bit: byte): LongInt;
       begin
         result := Numero and ((1 shl Bit) xor $FFFFFFFF);
       end;
    
       function CambiaBit(const Numero: longint; const Bit: byte): LongInt;
       begin
         result := Numero xor (1 shl Bit);
       end;