Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2022-02

Bit manipulation helpers

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote
I tried to use the bit helpers like https://www.freepascal.org/docs-html/rtl/sysutils/twordhelper.testbit.html but the bit to test is 0..15.

There are only 16 bits in a word, so that is correct.

Show quoted text 5 lines
I expected this to work like https://wiki.freepascal.org/Bit_manipulation but that doesn't seem to be the case. Does there exist bit helpers like GetBit (from the last link) in the RTL?

Yes, this is testbit.

Why do you say it does not work ?

Michael.

Reply by email

Ryan Joseph
to Michael Van Canneyt
Show quoted text 5 lines
On Feb 21, 2022, at 7:01 PM, Michael Van Canneyt via fpc-pascal wrote: Yes, this is testbit. Why do you say it does not work ?

Oh my, I was confusing my terms I think. I wanted to do bit masking (I think it's called). I was expecting there to be something like TestFlag in the RTL since I can never remember the syntax "Value = (Value or Index)"

function TestFlag(Value: QWord; Index: Byte): Boolean;
begin
  result := Value = (Value or Index);
end;
const
  KMOD_LCTRL = $0040;
  KMOD_RCTRL = $0080;
var
  flags: LongWord;
begin
  flags := KMOD_LCTRL + KMOD_RCTRL;
  writeln('TestBit: ', flags.TestBit(KMOD_LCTRL));
  writeln('TestFlag: ', TestFlag(flags, KMOD_LCTRL));

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 14 lines
> On Feb 21, 2022, at 7:01 PM, Michael Van Canneyt via fpc-pascal wrote: > > Yes, this is testbit. > > Why do you say it does not work ? Oh my, I was confusing my terms I think. I wanted to do bit masking (I think it's called). I was expecting there to be something like TestFlag in the RTL since I can never remember the syntax "Value = (Value or Index)" function TestFlag(Value: QWord; Index: Byte): Boolean; begin result := Value = (Value or Index); end;

Hm. I'm not sure that this code does what you need.

But masking is indeed not in the helpers.

Michael.

Reply by email

gabor
to Ryan Joseph: Oh my, I was confusing my terms I think. I wanted to do bit masking (I think it's…

W dniu 2022-02-21 o 13:18, Ryan Joseph via fpc-pascal pisze:

Show quoted text 6 lines
Oh my, I was confusing my terms I think. I wanted to do bit masking (I think it's called). I was expecting there to be something like TestFlag in the RTL since I can never remember the syntax "Value = (Value or Index)" function TestFlag(Value: QWord; Index: Byte): Boolean; begin result := Value = (Value or Index); end;

Should you use the "AND" operator instead of "OR" for bit testing?

result := Value = (Value AND Index);

Michał.

Reply by email

gabor
to gabor

W dniu 2022-02-21 o 13:34, gabor via fpc-pascal pisze:

Show quoted text 13 lines
W dniu 2022-02-21 o 13:18, Ryan Joseph via fpc-pascal pisze: > Oh my, I was confusing my terms I think. I wanted to do bit masking (I > think it's called). I was expecting there to be something like > TestFlag in the RTL since I can never remember the syntax "Value = > (Value or Index)" > function TestFlag(Value: QWord; Index: Byte): Boolean; > begin >    result := Value = (Value or Index); > end; Should you use the "AND" operator instead of "OR" for bit testing? result := Value = (Value AND Index);

Of course not. My mistake.
Michał.

Reply by email

Ryan Joseph
to Michael Van Canneyt: Hm. I'm not sure that this code does what you need. But masking is indeed not in the…
Show quoted text 6 lines
On Feb 21, 2022, at 7:31 PM, Michael Van Canneyt via fpc-pascal wrote: Hm. I'm not sure that this code does what you need. But masking is indeed not in the helpers.

Try it yourself, it does indeed work. How else are you supposed to test these kinds of bit flag APIs where all the flags are OR'd together into an LongWord? A type helper would be great because clearly this stuff is hard to remember.

Regards,
Ryan Joseph

Reply by email

Mattias Gaertner
to gabor: W dniu 2022-02-21 o 13:18, Ryan Joseph via fpc-pascal pisze: Should you use the "AND"…
Show quoted text 11 lines
W dniu 2022-02-21 o 13:18, Ryan Joseph via fpc-pascal pisze: > Oh my, I was confusing my terms I think. I wanted to do bit masking > (I think it's called). I was expecting there to be something like > TestFlag in the RTL since I can never remember the syntax "Value = > (Value or Index)" function TestFlag(Value: QWord; Index: Byte): > Boolean; begin result := Value = (Value or Index); > end; Should you use the "AND" operator instead of "OR" for bit testing? result := Value = (Value AND Index);

No.

result := (Byte(Value) AND Index)>0;
result := Index = (Byte(Value) AND Index);
result := Byte(Value) = (Byte(Value) OR Index);

Mattias

Reply by email