Sign in Join

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

Mailing lists / fpc-devel / 2013-11

Re: TBits problem

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Andrea Mauri

any hint?
should I have to open a bug report?

Il 31/10/2013 16:23, Andrea Mauri ha scritto:

Show quoted text 62 lines
Dear All, I am using TBits object but I found some errors using it. Before adding a bugreport to the bugtracker I would be sure that there is nothing that I am doing wrong. Below a small procedure that highlights the wrong behaviour of TBits object. var i, bsize1, bsize2: integer; b1, b2, b3: TBits; s1, s2, s3: string; begin bsize1:= 16; bsize2:= 164; b1:= TBits.Create; b2:= TBits.Create(bsize2); b3:= TBits.Create; for i:= 0 to bsize1 - 1 do b1[i]:= True; b2.OrBits(b1); b3.OrBits(b1); for i:= b1.Size to bsize2 - 1 do b3[i]:= False; if not b2.Equals(b3) then begin s1:= bitstostring(b1); s2:= bitstostring(b2); s3:= bitstostring(b3); end; b1.Free; b2.Free; b3.Free; where bitstostring is a simple function as below: function bitstostring(b:TBits): string; var i: integer; bs: string; begin bs:= ''; for i:= 0 to b.Size - 1 do if b[i] then bs:= bs + '1' else bs:= bs + '0'; Result:= bs; end; What I got is that b2 has set to 1 not only the bits that are 1 in b1 but also some other bits. b2 and b3 should be the same but they are not. I tested it on Win32 using lazarus: Lazarus 1.0.13 r42910M FPC 2.6.2 i386-win32-win32/win64 Could you please let me know if you can reproduce it or if I am doing something wrong? Best regards, Andrea Mauri

Reply by email

Flávio Etrusco
to Andrea Mauri
Andrea Mauri wrote 70 lines
any hint? should I have to open a bug report? Il 31/10/2013 16:23, Andrea Mauri ha scritto: > > Dear All, > I am using TBits object but I found some errors using it. Before adding > a bugreport to the bugtracker I would be sure that there is nothing that > I am doing wrong. > Below a small procedure that highlights the wrong behaviour of TBits > object. > > var > i, bsize1, bsize2: integer; > b1, b2, b3: TBits; > s1, s2, s3: string; > begin > bsize1:= 16; > bsize2:= 164; > b1:= TBits.Create; > b2:= TBits.Create(bsize2); > b3:= TBits.Create; > for i:= 0 to bsize1 - 1 do > b1[i]:= True; > > b2.OrBits(b1); > b3.OrBits(b1); > for i:= b1.Size to bsize2 - 1 do > b3[i]:= False; > if not b2.Equals(b3) then > begin > s1:= bitstostring(b1); > s2:= bitstostring(b2); > s3:= bitstostring(b3); > end; > b1.Free; > b2.Free; > b3.Free; > > where bitstostring is a simple function as below: > > function bitstostring(b:TBits): string; > var > i: integer; > bs: string; > begin > bs:= ''; > for i:= 0 to b.Size - 1 do > if b[i] then > bs:= bs + '1' > else > bs:= bs + '0'; > Result:= bs; > end; > > What I got is that b2 has set to 1 not only the bits that are 1 in b1 > but also some other bits. b2 and b3 should be the same but they are not. > > I tested it on Win32 using lazarus: > Lazarus 1.0.13 r42910M FPC 2.6.2 i386-win32-win32/win64 > > Could you please let me know if you can reproduce it or if I am doing > something wrong? > > Best regards, > Andrea Mauri > _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

If you send a minimal test-case and actual results you might have a better chance at getting a reply...

Best regards,
Flávio

Reply by email

Andrea Mauri
to Flávio Etrusco
If you send a minimal test-case and actual results you might have a better chance at getting a reply...

Below a sample test project.
I create a TBits (b1) and then I set 16 bits as 1. Then I create 2 TBits (b2 and b3) with size 164, I use .OrBits to OR b1 with b2 and b3. I expect that b2 and b3 will result in a TBits with size 164 but with only first 16 bits as 1. What I got is different. A lot of bits between bits[16] and bits[163] are set to 1.
You can easily check it using the following test project. Tested on windows fpc 2.6.2.

program Project1;
{$mode objfpc}{$H+}
uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, SysUtils, CustApp
   { you can add units after this };
type
   { TMyApplication }
   TMyApplication = class(TCustomApplication)
   protected
     procedure DoRun; override;
   public
     constructor Create(TheOwner: TComponent); override;
     destructor Destroy; override;
     procedure WriteHelp; virtual;
     function bitstostring(b: TBits): string;
   end;

{ TMyApplication }

procedure TMyApplication.DoRun;
var
   ErrorMsg: String;
   i, bsize1, bsize2: integer;
   b1, b2, b3: TBits;
   s1, s2, s3: string;
begin
   // quick check parameters
   ErrorMsg:=CheckOptions('h','help');
   if ErrorMsg'' then begin
     ShowException(Exception.Create(ErrorMsg));
     Terminate;
     Exit;
   end;
   // parse parameters
   if HasOption('h','help') then begin
     WriteHelp;
     Terminate;
     Exit;
   end;
   { add your program here }
   bsize1:= 16;
   bsize2:= 164;
   b1:= TBits.Create;
   b2:= TBits.Create(bsize2);
   b3:= TBits.Create(bsize2);
   for i:= 0 to bsize1 - 1 do
     b1[i]:= True;
   b2.OrBits(b1);
   b3.OrBits(b1);
   for i:= b1.Size to bsize2 - 1 do
     b3[i]:= False;
   if not b2.Equals(b3) then
   begin
     writeln(bitstostring(b1));
     writeln(bitstostring(b2));
     writeln(bitstostring(b3));
   end;
   b1.Free;
   b2.Free;
   b3.Free;
   // stop program loop
   Terminate;
end;
constructor TMyApplication.Create(TheOwner: TComponent);
begin
   inherited Create(TheOwner);
   StopOnException:=True;
end;
destructor TMyApplication.Destroy;
begin
   inherited Destroy;
end;
procedure TMyApplication.WriteHelp;
begin
   { add your help code here }
   writeln('Usage: ',ExeName,' -h');
end;
function TMyApplication.bitstostring(b: TBits): string;
var
   i: integer;
   bs: string;
begin
   bs:= '';
   for i:= 0 to b.Size - 1 do
     if b[i] then
       bs:= bs + '1'
     else
       bs:= bs + '0';
   Result:= bs;
end;
var
   Application: TMyApplication;
begin
   Application:=TMyApplication.Create(nil);
   Application.Title:='My Application';
   Application.Run;
   Application.Free;
end.

Reply by email

Tomas Hajny
to Andrea Mauri: any hint? should I have to open a bug report? Il 31/10/2013 16:23, Andrea Mauri ha…
Andrea Mauri wrote
any hint? should I have to open a bug report?

Yes, I think so (although I have no clue regarding compatibility with Delphi, etc.) - looking at the implementation of OrBits (and also XorBits), the calculation which bits should be used is different from AndBits (and probably wrong - it checks if the size allocated for the current object is sufficient for covering the bits in argument, but it does not care about the initialized size of the argument - as long as the allocated size is sufficient, it does not crash, but it gives wrong results).

Tomas

Show quoted text 63 lines
Il 31/10/2013 16:23, Andrea Mauri ha scritto: > Dear All, > I am using TBits object but I found some errors using it. Before adding > a bugreport to the bugtracker I would be sure that there is nothing that > I am doing wrong. > Below a small procedure that highlights the wrong behaviour of TBits > object. > > var > i, bsize1, bsize2: integer; > b1, b2, b3: TBits; > s1, s2, s3: string; > begin > bsize1:= 16; > bsize2:= 164; > b1:= TBits.Create; > b2:= TBits.Create(bsize2); > b3:= TBits.Create; > for i:= 0 to bsize1 - 1 do > b1[i]:= True; > > b2.OrBits(b1); > b3.OrBits(b1); > for i:= b1.Size to bsize2 - 1 do > b3[i]:= False; > if not b2.Equals(b3) then > begin > s1:= bitstostring(b1); > s2:= bitstostring(b2); > s3:= bitstostring(b3); > end; > b1.Free; > b2.Free; > b3.Free; > > where bitstostring is a simple function as below: > > function bitstostring(b:TBits): string; > var > i: integer; > bs: string; > begin > bs:= ''; > for i:= 0 to b.Size - 1 do > if b[i] then > bs:= bs + '1' > else > bs:= bs + '0'; > Result:= bs; > end; > > What I got is that b2 has set to 1 not only the bits that are 1 in b1 > but also some other bits. b2 and b3 should be the same but they are not. > > I tested it on Win32 using lazarus: > Lazarus 1.0.13 r42910M FPC 2.6.2 i386-win32-win32/win64 > > Could you please let me know if you can reproduce it or if I am doing > something wrong? > > Best regards, > Andrea Mauri

Reply by email

Andrea Mauri
to Tomas Hajny

Il 06/11/2013 18:41, Tomas Hajny ha scritto:

Show quoted text 8 lines
Yes, I think so (although I have no clue regarding compatibility with Delphi, etc.) - looking at the implementation of OrBits (and also XorBits), the calculation which bits should be used is different from AndBits (and probably wrong - it checks if the size allocated for the current object is sufficient for covering the bits in argument, but it does not care about the initialized size of the argument - as long as the allocated size is sufficient, it does not crash, but it gives wrong results).

BugReport opened as bureportid: 0025289

Reply by email