Difference between revisions of "Converting Bytes to Bits"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m (Created page with '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 [[Bina…')
 
m (Text replacement - "Qbasic" to "QBasic")
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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.
Although QB64 has [[_BYTE]] and [[_BIT]] variable types, there may be times that you just want to know which bits are on or off in the byte value or convert the value to a [[Binary]] number.


The tutorial below shows you how to do things manually, for instructional purposes. Bear in mind, though, that QB64 has the following suite of commands aimed at working with bits:
:: [[_SETBIT]]
:: [[_READBIT]]
:: [[_TOGGLEBIT]]
:: [[_RESETBIT]]
:: [[_SHR]], [[_SHL]]
== Doing it yourself ==


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





Latest revision as of 13:10, 10 February 2021

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

The tutorial below shows you how to do things manually, for instructional purposes. Bear in mind, though, that QB64 has the following suite of commands aimed at working with bits:

_SETBIT
_READBIT
_TOGGLEBIT
_RESETBIT
_SHR, _SHL

Doing it yourself

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 Bin$ = Bin$ + "1" ELSE: PAINT (640 - (37 * (i + 1)), 189), 0, 9 Bin$ = Bin$ + "0" END IF NEXT COLOR 10: LOCATE 16, 50: PRINT "Binary ="; VAL(Bin$) COLOR 9: LOCATE 16, 10: PRINT "Decimal ="; Num;: COLOR 13: PRINT " Hex = "; Hexa$ Hexa$ = "": Bin$ = "" 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:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page