Jump to content
A.M. Hoornweg

64 bit shift operations?

Recommended Posts

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

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;

 

  • Like 1

Share this post


Link to post

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 by Brian Evans

Share this post


Link to post
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×