Converting Bytes to Bits: Difference between revisions
Jump to navigation
Jump to search
Code by Ted Weissgerber
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Removed protection from "Converting Bytes to Bits") |
No edit summary |
||
Line 6: | Line 6: | ||
: 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 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}} |
Revision as of 01:23, 23 January 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 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 |
See also: