Results 1 to 10 of 10

Thread: How to manipulate specific bits of a variable

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by paul_nicholls View Post
    Using freepascal, you can now have bit packed records like so:

    Code:
    {
        Flag register:
        ---------------
        Bit[0] - Reserved
        Bit[1] - c (Unsigned Carry and Unsigned Borrow flag)
        Bit[2] - z (Zero flag)
        Bit[3] - Reserved
        Bit[4] - Reserved
        Bit[5] - Reserved
        Bit[6] - o (Signed Overflow Flag)
        Bit[7] - n (Negative Flag; aka Sign Flag)
    }
    
    
    type
      TBit  = 0..1;
      TFlags = bitpacked record
        _0 : TBit;
         c : TBit;
         z : TBit;
        _3 : TBit;
        _4 : TBit;
        _5 : TBit;
         o : TBit;
         n : TBit;
      end;
    here, you can access all the separate bits as a separate variable in the record...rather neat!
    It isn't supported under Delphi unfortunately, but if you only use freepascal, then great
    Does variable names here actually bears any importance or could I just write:
    Code:
    type
      TBit  = 0..1;
      TByte = bitpacked record //TByte to avoid confusion with actual Byte type
        b1 : TBit;
        b2 : TBit;
        b3 : TBit;
        b4 : TBit;
        b5 : TBit;
        b6 : TBit;
        b7 : TBit;
        b8 : TBit;
      end;
    //edit: I wrote (but didn't test) function that would check if specific bit is set. You'll need math unit for that one:
    Code:
    function IsBitSet(num:integer;bit:byte):Boolean
    var bitnum:Integer;
    begin
      bitnum:= Trunc(intpower(2,bit);
      Result:= (num and bitnum) = bitnum;
    end;
    Though it should work fine.
    Last edited by Darkhog; 14-07-2013 at 03:23 PM.

  2. #2
    Quote Originally Posted by Darkhog View Post
    //edit: I wrote (but didn't test) function that would check if specific bit is set. You'll need math unit for that one:
    Code:
    function IsBitSet(num:integer;bit:byte):Boolean
    var bitnum:Integer;
    begin
      bitnum:= Trunc(intpower(2,bit);
      Result:= (num and bitnum) = bitnum;
    end;
    Though it should work fine.
    Dan already gave more optimized code for that
    Code:
    function GetBit(const Value, Bit: LongWord): Boolean;
    begin
      Result := Boolean((Value shr Bit) and 1);
    end;
    Both trunc() and intpower() do a massive amount of extra work.

  3. #3
    I guess it should work fine too. But why using longword for bit no.? Byte would do here much better and I don't suppose we'll need to check like 5000th bit of value soon. Therefore longword would just waste memory while byte would be much more memory-efficient here, especially when dealing with embedded systems.

  4. #4
    Byte would be worse actually - more generated code. And you wouldn't save any memory, as it would be most likely passed in a register or pushed as a 4B value on the stack, depending on ABI.

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
  •