Converting Bytes to Bits: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "Converting Bytes to Bits" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:




: The following example shows how to convert an [[_UNSIGNED]] [[_BYTE]] or [[INTEGER]] value(0 to 255) to a [[Binary]] [[STRING]] number in Qbasic.
: The following example shows how to convert an [[_UNSIGNED]] [[_BYTE]] or [[INTEGER]] value(0 to 255) to a [[Binary]] [[STRING]] number in QBasic.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} byte as {{Cl|_UNSIGNED}} {{Cl|_BYTE}}
{{Cl|DIM}} byte as {{Cl|_UNSIGNED}} {{Cl|_BYTE}}


Line 13: Line 13:
{{Cl|FOR...NEXT|FOR}} bit = 7 to 0 {{Cl|STEP}} -1
{{Cl|FOR...NEXT|FOR}} bit = 7 to 0 {{Cl|STEP}} -1
   {{Cl|IF...THEN|IF}} byte {{Cl|AND (boolean)|AND}} 2 ^ bit {{Cl|THEN}} {{Cl|PRINT}} "1"; {{Cl|ELSE}} {{Cl|PRINT}} "0";
   {{Cl|IF...THEN|IF}} byte {{Cl|AND (boolean)|AND}} 2 ^ bit {{Cl|THEN}} {{Cl|PRINT}} "1"; {{Cl|ELSE}} {{Cl|PRINT}} "0";
{{Cl|NEXT}} '' ''
{{Cl|NEXT}}
{{CodeEnd}}
{{CodeEnd}}
: ''Notes:'' The above code can be adapted to place a value into a bit [[Arrays|Array]] for up to 8 flag values in a single Byte.
: ''Notes:'' The above code can be adapted to place a value into a bit [[Arrays|Array]] for up to 8 flag values in a single Byte.




: How upper and lower [[_BYTE|byte]] bits are read from an [[INTEGER]] value using whole decimal or [[HEX$|hexadecimal]] values.  
: How upper and lower [[_BYTE|byte]] bits are read from an [[INTEGER]] value using whole decimal or [[HEX$|hexadecimal]] values.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12


Line 35: Line 35:
       {{Cl|IF...THEN|IF}} (Num {{Cl|AND (boolean)|AND}} 2 ^ i) {{Cl|THEN}}
       {{Cl|IF...THEN|IF}} (Num {{Cl|AND (boolean)|AND}} 2 ^ i) {{Cl|THEN}}
         {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 12, 9
         {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 12, 9
         Bin$ = Bin$ + "1"
         BinStr$ = BinStr$ + "1"
       {{Cl|ELSE}}: {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 0, 9
       {{Cl|ELSE}}: {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 0, 9
         Bin$ = Bin$ + "0"
         BinStr$ = BinStr$ + "0"
       {{Cl|END IF}}
       {{Cl|END IF}}
     {{Cl|NEXT}}
     {{Cl|NEXT}}
     {{Cl|COLOR}} 10: {{Cl|LOCATE}} 16, 50: {{Cl|PRINT}} "Binary ="; {{Cl|VAL}}(Bin$)
     {{Cl|COLOR}} 10: {{Cl|LOCATE}} 16, 50: {{Cl|PRINT}} "Binary ="; {{Cl|VAL}}(BinStr$)
     {{Cl|COLOR}} 9: {{Cl|LOCATE}} 16, 10: {{Cl|PRINT}} "Decimal ="; Num;: {{Cl|COLOR}} 13: {{Cl|PRINT}} "      Hex = "; Hexa$
     {{Cl|COLOR}} 9: {{Cl|LOCATE}} 16, 10: {{Cl|PRINT}} "Decimal ="; Num;: {{Cl|COLOR}} 13: {{Cl|PRINT}} "      Hex = "; Hexa$
     Hexa$ = "": Bin$ = ""
     Hexa$ = "": BinStr$ = ""
   {{Cl|END IF}}
   {{Cl|END IF}}
   {{Cl|COLOR}} 14: {{Cl|LOCATE}} 17, 15: {{Cl|INPUT}} "Enter a decimal or HEX({{Cl|&H}}) value (0 Quits): ", frst$
   {{Cl|COLOR}} 14: {{Cl|LOCATE}} 17, 15: {{Cl|INPUT}} "Enter a decimal or HEX({{Cl|&H}}) value (0 Quits): ", frst$
Line 54: Line 54:
   Num = first + second
   Num = first + second
   Hexa$ = "{{Cl|&H}}" + {{Cl|HEX$}}(Num)
   Hexa$ = "{{Cl|&H}}" + {{Cl|HEX$}}(Num)
{{Cl|LOOP}} {{Cl|UNTIL}} first = 0 {{Cl|OR (boolean)|OR}} Num > 32767 {{Cl|OR (boolean)|OR}} Num < -32768 {{Cl|OR (boolean)|OR}} (Num < -1 {{Cl|AND (boolean)|AND}} Num > -32768 '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} first = 0 {{Cl|OR (boolean)|OR}} Num > 32767 {{Cl|OR (boolean)|OR}} Num < -32768 {{Cl|OR (boolean)|OR}} (Num < -1 {{Cl|AND (boolean)|AND}} Num > -32768
{{Cl|END}}
{{Cl|END}}
{{CodeEnd}}{{small|Code by Ted Weissgerber}}
{{CodeEnd}}
{{Small|Code by Ted Weissgerber}}




''See also:''
{{PageSeeAlso}}
* [[Binary]]
* [[Binary]]
* [[_SHL]], [[_SHR]]
* [[_SHL]], [[_SHR]]

Latest revision as of 21:19, 5 April 2023

Although QB64 has _BYTE and _BIT variable types, there may be times that you just want to know which bits are on of off in the byte value or convert the value to a Binary number.


Bits are numbered from 0 to 7 and normally are read from the most significant bit(MSB = 7) to the least significant bit(LSB = 0).


The following example shows how to convert an _UNSIGNED _BYTE or INTEGER value(0 to 255) to a Binary STRING number in QBasic.
DIM byte as _UNSIGNED _BYTE

byte = 222

FOR bit = 7 to 0 STEP -1
  IF byte AND 2 ^ bit THEN PRINT "1"; ELSE PRINT "0";
NEXT
Notes: The above code can be adapted to place a value into a bit Array for up to 8 flag values in a single Byte.


How upper and lower byte bits are read from an INTEGER value using whole decimal or hexadecimal values.
SCREEN 12

COLOR 11: LOCATE 10, 2: PRINT "      AH (High Register Byte Bits)           AL (Low Register Byte Bits)"
COLOR 14: LOCATE 11, 2: PRINT "    15   14  13   12   11  10    9   8    7   6    5   4    3    2   1    0"
COLOR 13: LOCATE 14, 2: PRINT " &H8000 4000 2000 1000 800 400  200 100  80   40  20   10   8    4   2  &H1"
COLOR 11: LOCATE 15, 2: PRINT "-32768 16384 8192 4096 2048 1024 512 256 128  64  32   16   8    4   2    1"
FOR i = 1 TO 16
  CIRCLE (640 - (37 * i), 189), 8, 9 'place bit circles
NEXT
LINE (324, 160)-(326, 207), 11, BF 'line splits bytes
DO
  IF Num THEN
    FOR i = 15 TO 0 STEP -1
      IF (Num AND 2 ^ i) THEN
        PAINT (640 - (37 * (i + 1)), 189), 12, 9
        BinStr$ = BinStr$ + "1"
      ELSE: PAINT (640 - (37 * (i + 1)), 189), 0, 9
        BinStr$ = BinStr$ + "0"
      END IF
    NEXT
    COLOR 10: LOCATE 16, 50: PRINT "Binary ="; VAL(BinStr$)
    COLOR 9: LOCATE 16, 10: PRINT "Decimal ="; Num;: COLOR 13: PRINT "       Hex = "; Hexa$
    Hexa$ = "": BinStr$ = ""
  END IF
  COLOR 14: LOCATE 17, 15: INPUT "Enter a decimal or HEX(&H) value (0 Quits): ", frst$
  first = VAL(frst$)
  LOCATE 17, 15: PRINT SPACE$(55)
  IF first THEN
    COLOR 13: LOCATE 17, 15: INPUT "Enter a second value: ", secnd$
    second = VAL(secnd$)
    LOCATE 16, 10: PRINT SPACE$(69)
  END IF
  Num = first + second
  Hexa$ = "&H" + HEX$(Num)
LOOP UNTIL first = 0 OR Num > 32767 OR Num < -32768 OR (Num < -1 AND Num > -32768
END
Code by Ted Weissgerber


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage