Results 1 to 10 of 10

Thread: How to manipulate specific bits of a variable

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Code:
    function GetBit(const Value, Bit: LongWord): Boolean;
    begin
      Result := Boolean((Value shr Bit) and 1);
    end;
    
    function SetBit(const Value, Bit: LongWord; const NewBitValue: Boolean): LongWord;
    beign
      Result := (Value and not LongWord(1 shl Bit)) or (LongWord(NewBitValue) shl Bit)
    end;
    there may be some errors in the code but generally it should work. Value is the 32 bit combination that you are working with, Bit is the order of the bit that you want to read/change.
    However, in pascal there is a more convenient way of working with bit flags - sets. they are very neatly designed, support up to 128 bits and compile into the same optimized binary math.
    Last edited by Dan; 12-07-2013 at 05:39 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •