"Parsing" through bits in a byte 
Author Message
 "Parsing" through bits in a byte

I have this dream...

I suddenly find myself reading information from a serial device, and I
need to be able to determine which bits are ON in two bytes I receive.

Each of the bits represents a specific status message which is true if
the bit is ON.  Any combination of status messages is possible.  I
wonder dreamily, How do I "parse" through a byte (char) to determine
which bits are ON?

Then I realize I'm not dreaming, and I need to do this yesterday!!!

All responses rewarded with good Karma.

Thanks in advance,
David M. Goncalves



Tue, 02 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte

Quote:

> How do I "parse" through a byte (char) to determine
> which bits are ON?

AND and test.

Set up a bitmask with a bit ON in the desired position. AND it
with the byte, and test if the result equals the bitmask. If it
does, then the bit is on. If it doesn't, the bit is off.

Example-

CONST
  bitmask: BYTE = $2 (* 00000010 *)

VAR
  testbyte: BYTE;

BEGIN
  ............
  IF testbyte AND bitmask = bitmask THEN
    (* bit in testbyte is ON *)
  ELSE
    (* bit in testbyte is OFF *)
END.

Regards,
Brian.  
--
Corbin Network Services
Box 1960, Sparwood, BC, Canada
Vox: 250.425.7471
Fax: 250.425.7472
Brian Grainger, Owner

Member, Canadian Association of Internet Providers
Network operations, consulting, programming, and training



Tue, 02 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte


Quote:
>I have this dream...
>I suddenly find myself reading information from a serial device, and I
>need to be able to determine which bits are ON in two bytes I receive.
>Each of the bits represents a specific status message which is true if
>the bit is ON.  Any combination of status messages is possible.  I
>wonder dreamily, How do I "parse" through a byte (char) to determine
>which bits are ON?
>Then I realize I'm not dreaming, and I need to do this yesterday!!!
>All responses rewarded with good Karma.
>Thanks in advance,
>David M. Goncalves

{ You did not mention which Pascal you were using. The demo below
only accepts the printable keys from [' '..'~'] so it should work
with most versions. Technically a byte is a positive integer [0..255]
instead of a char but both are used in this program.

The following "quicky" parses from the least significant bit to the
most and puts an integer 1 or 0 in an 8 bit array. In this case an
"AND mask" having a single 1 is used to extract each bit. A CHAR is
used as an input and ORD(CHAR) is the byte parsed into the array.


Program ParsBits;
Label finis;

VAR
bit:Array[1..8] of Integer;
n, mask, xin:Integer;
ch:Char;

Begin
     Writeln; Writeln;
Repeat
     Write('(Press Enter twice to quit.)  Enter a Character: ');
     Readln(ch);

     If Not(ch in [' '.. '~'] ) then Goto finis;

     xin := Ord(ch);   mask := 1;
     For n := 8 downto 1 Do
     Begin
          If (mask AND xin) > 0 Then bit[n] := 1
          Else bit[n] := 0;
          mask := 2 * mask;  {Turbo's SHL could be used}
     End;

     Write('The ASCII value of "', ch, '" is ', xin, ' which is: ');
     For n := 1 to 8 Do Write(bit[n]);
     Writeln; Writeln;
finis:
Until Not(ch in [' '.. '~'] );
END.



Tue, 02 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte

IsBitSet:= (TheByte and (1 shl TheBitNumberYouWantToCheck)) <> 0;

Where TheBitNumberYouWantToCheck is from 0..7

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/



Tue, 02 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte

Quote:

> I have this dream...

> I suddenly find myself reading information from a serial device, and I
> need to be able to determine which bits are ON in two bytes I receive.

> Each of the bits represents a specific status message which is true if
> the bit is ON.  Any combination of status messages is possible.  I
> wonder dreamily, How do I "parse" through a byte (char) to determine
> which bits are ON?

> Then I realize I'm not dreaming, and I need to do this yesterday!!!

> All responses rewarded with good Karma.

> Thanks in advance,
> David M. Goncalves

Here's a byte   1   0   1  0   1  0  1  0
                128 64  32 16  8  4  2  1

   1 and 1 = 0
   1 and 2 = 2  (bit set)
   1 and 4 = 0  
   1 and 8 = 8  (bit set)
   1 and 16 = 0
   1 and 32 = 32 (bit set)
   1 and 64 = 0
   1 and 128 = 128 (bit set)

  if (1 and 1)=1 then  bitoneset := true else bitoneset := false;

BTW, I never said I was easy to understand :).



Tue, 02 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte

Quote:

> I suddenly find myself reading information from a serial device, and I
> need to be able to determine which bits are ON in two bytes I receive.

> Each of the bits represents a specific status message which is true if
> the bit is ON.  Any combination of status messages is possible.  I
> wonder dreamily, How do I "parse" through a byte (char) to determine
> which bits are ON?

     Just shows you that given any (homework?) problem, there are
multiple ways to solve it.  My own preference (particularly when dealing
with a real hardware device) is to use enumerated types and sets.  I
name the bits (e.g. statusregisterbit = (readybit, interruptbit, dmabit,
countdownbit, quadruplespeedbit, doublespeedbit, repeatbit, gobit)) and
then make the register a set of bits.  Testing, setting, and clearing
bits then become simple set operations.  Almost intuitive ...

Bob Schor
Pascal Enthusiast



Wed, 03 Nov 1999 03:00:00 GMT  
 "Parsing" through bits in a byte

On Fri, 16 May 1997 10:10:16 -0400, "David M. Goncalves"

Quote:

>I have this dream...

>I suddenly find myself reading information from a serial device, and I
>need to be able to determine which bits are ON in two bytes I receive.

>Each of the bits represents a specific status message which is true if
>the bit is ON.  Any combination of status messages is possible.  I
>wonder dreamily, How do I "parse" through a byte (char) to determine
>which bits are ON?

>Then I realize I'm not dreaming, and I need to do this yesterday!!!

>All responses rewarded with good Karma.

>Thanks in advance,
>David M. Goncalves

Use the logical operator AND with the successive single "1" integers.
These are 1,2,4,8,16,32,64,128. If you do a logical AND with each of
these, only if the bit you are testing is a "1" will a non-zero value
be returned.

Frank Wood



Thu, 04 Nov 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. The keys "-"/"+"/"*"

2. ""256 Color Mouse""

3. FP106: Error: Incompatible types: got "BYTE" expected "SHORTSTRING"

4. What are "Denormals" and "Nans"

5. Utility to convert "asm" to "inline( )"

6. "delete from" followed by "insert into" = AV

7. "Replace Connection", "Unable to connect to: PARADOX".

8. HELP!!: "Multiple net files","Lock file has grown too large"

9. A problem in creating equal sized "packets" from a set of "packages".

10. How to make a "xxx.EXE" from the "xxx.PAS"???

11. "blob" and "DBT" file errors

12. ComboBox that shows "Name" and returns "Value"?

 

 
Powered by phpBB® Forum Software