Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread gabor via fpc-pascal


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

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ł.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Mattias Gaertner via fpc-pascal
On Mon, 21 Feb 2022 13:34:29 +0100
gabor via fpc-pascal  wrote:

> 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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Ryan Joseph via fpc-pascal


> 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

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread gabor via fpc-pascal

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);

Michał.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Michael Van Canneyt via fpc-pascal



On Mon, 21 Feb 2022, Ryan Joseph via fpc-pascal wrote:





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.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Ryan Joseph via fpc-pascal


> 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

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Michael Van Canneyt via fpc-pascal



On Mon, 21 Feb 2022, Ryan Joseph via fpc-pascal 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.


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.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Bit manipulation helpers

2022-02-21 Thread Ryan Joseph via fpc-pascal
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. 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?

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal