Jump to content
Sign in to follow this  
M.Joos

TInterlocked.BitTestAndSet strangeness

Recommended Posts

Hi all, when running the following code

program Project105;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.SyncObjs;
var
  Sync: Integer;
  Test: Boolean;

begin
  try
    TInterlocked.BitTestAndClear(Sync,0);
    Test := TInterlocked.BitTestAndSet(Sync,0);
    if Test then
      writeln('Why is Test true here ??') else
      writeln('Okay, That''s what I expected');
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Why is it that the variable Test is set to true, when the documentation says  ".... The method returns True if the bit is 1, False otherwise ". Clearly I have set the bit to 0 with TInterlocked.BitTestandClear, haven't I ? What am I missing.

Tested with Delphi 10.1 Berlin.

Edited by M.Joos

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Works as expected in Rio...

You are right, Uwe. Tested it in 10.3.2 and it works as expected.

Upon closer inspection it seems they fixed the Tinterlocked code which in Berlin looked like this:

$ELSEIF Defined(X86ASM)}
asm
  AND   EDX,31
  LOCK BTS   [EAX],EDX
  SETNC  AL
end;
{$ELSE}
var

and in Rio looks like this:

{$ELSEIF Defined(X86ASM)}
asm
  AND   EDX,31
  LOCK BTS   [EAX],EDX
  SETC  AL
end;
{$ELSE}

Okay, and I thought I would be crazy or something

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
Sign in to follow this  

×