home *** CD-ROM | disk | FTP | other *** search
- {$D-}
- unit Binary;
-
- interface
-
- function ShowBits(B : Byte): string;
- procedure SetBit(Position: Integer; Value : Byte; var ChangeByte : Byte);
- function BitOn(Position: Integer; TestByte: Byte) : Boolean;
-
- implementation
-
- { This function accepts a byte parameter and returns
- a string of eight ones and zeros indicating the
- binary form of a bite. }
- function ShowBits(B : Byte): string;
- var
- i: Integer;
- bt: Byte;
- s: string;
- begin
- bt := $01;
- s := '';
- for i := 1 to 8 do begin
- if (b And bt) > 0 then
- S := '1' + s
- else
- s := '0' + s;
- {$R-}
- bt := bt shl 1;
- {$R+}
- end;
- ShowBits := s;
- end;
-
- { This procedure sets a particular bit in the byte changebyte
- to either 1 or 0. The bit is specified by Position, which can range
- from 0 to 7. In Value, put 1 if you want the bit Position set to 1
- and put 0 if you want bit Position set to 0. The right byte is Position
- 0, the far left is Position 7. Based on a routine found in
- Turbo Pascal by Stephen K O'Brian. }
- procedure SetBit(Position : Integer; Value : Byte; var ChangeByte : Byte);
- var
- Bt : Byte;
- begin
- bt := $01;
- bt := bt shl Position;
- if Value = 1 then
- ChangeByte := ChangeByte or bt
- else begin
- bt := bt xor $FF;
- ChangeByte := ChangeByte and bt;
- end;
- end;
-
- { This function tests if a bit in TestByte is turned on (equal to 1).
- If the bit indicated by Position is turned on, then BitOn returns True. }
- function BitOn(Position : Integer; TestByte : Byte) : Boolean;
- var
- bt : Byte;
- begin
- bt := $01;
- bt := bt shl Position;
- BitOn := (bt and TestByte) > 0;
- end;
-
- end.