A.M. Hoornweg 144 Posted July 6, 2022 Hello all, I have the impression that Delphi's SHL operator only handles 32 bits even when compiling for 64-bit. Does anybody have an alternative routine that handles 64 bits? function bit(idx,value: uint64): Boolean; begin Result := ((1 shl idx) and value) <> 0; end; procedure test; var i:uint64; b:boolean; begin i:=$ffffffff00000000; b:=bit(33,i); // Should return TRUE but returns FALSE end; Share this post Link to post
PeterBelow 238 Posted July 6, 2022 38 minutes ago, A.M. Hoornweg said: Hello all, I have the impression that Delphi's SHL operator only handles 32 bits even when compiling for 64-bit. Does anybody have an alternative routine that handles 64 bits? function bit(idx,value: uint64): Boolean; begin Result := ((1 shl idx) and value) <> 0; end; procedure test; var i:uint64; b:boolean; begin i:=$ffffffff00000000; b:=bit(33,i); // Should return TRUE but returns FALSE end; Your test case is in error, your value i has all top 32 bits set to zero. Share this post Link to post
Lars Fosdal 1792 Posted July 6, 2022 Maybe you need to cast the 1 to uint64 too? Edit: Checked - It works with function bit(idx,value: uint64): Boolean; begin Result := ((uint64(1) shl idx) and value) <> 0; end; 1 Share this post Link to post
Lars Fosdal 1792 Posted July 6, 2022 So, it was not the operator, but the type the operator operated on. Share this post Link to post
Brian Evans 105 Posted July 6, 2022 (edited) Type the constant by replacing 1 with UInt64(1) in your bit() function. Works in 32 and 64 bit targets. Result := ((Uint64(1) shl idx) and value) <> 0; The reason is bit shifts are modulo the size of the type of the arguments. So no error and you get short shifted when one argument is 32 bits which is the default for integer constants. Edited July 6, 2022 by Brian Evans Share this post Link to post
A.M. Hoornweg 144 Posted July 6, 2022 48 minutes ago, PeterBelow said: Your test case is in error, your value i has all top 32 bits set to zero. It has the 32 LSB's set to zero. Bits 32...63 are set to one and I'm testing bit 33. Share this post Link to post
David Heffernan 2345 Posted July 6, 2022 Imagine if we could specify the type of a literal ........ Share this post Link to post