Binary: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 13: Line 13:


<center>''Decimal Bit values returned when a bit is on:''</center>
<center>''Decimal Bit values returned when a bit is on:''</center>
{{WhiteStart}}                                    '''Exponent of 2 and Bit #  7 6 5 4 3 2 1 0 '''
{{FixedStart}}                                    '''Exponent of 2 and Bit #  7 6 5 4 3 2 1 0 '''


                       Bit 0 = 2 ^ 0 = 1 decimal    binary = 0 0 0 0 0 0 0 1
                       Bit 0 = 2 ^ 0 = 1 decimal    binary = 0 0 0 0 0 0 0 1
Line 25: Line 25:


                       All_Bits_On = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255
                       All_Bits_On = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255
{{WhiteEnd}}
{{FixedEnd}}


: ''Explanation:'' The table above only displays the binary value of each bit when on. If a value of 255 was read, the binary number = 11111111 (all bits on). If only bits 2, 3 and 4 are on then the value would be 4 + 8 + 16 = 28 or 00011100.
: ''Explanation:'' The table above only displays the binary value of each bit when on. If a value of 255 was read, the binary number = 11111111 (all bits on). If only bits 2, 3 and 4 are on then the value would be 4 + 8 + 16 = 28 or 00011100.
Line 33: Line 33:
<center>''' Truth table of the BASIC Logical Operators:'''</center>
<center>''' Truth table of the BASIC Logical Operators:'''</center>


{{LogicalTruthTable}}
{{LogicalTruthPlugin}}




Line 40: Line 40:


:''Example 1:'' Converting decimal values to a string binary value. Use [[LOCATE]] before the SUB call to locate the [[PRINT]].
:''Example 1:'' Converting decimal values to a string binary value. Use [[LOCATE]] before the SUB call to locate the [[PRINT]].
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DO...LOOP|DO}}
{{Cl|DO}}
  {{Cl|LOCATE}} 2, 10
    {{Cl|LOCATE}} {{Text|2|#F580B1}}, {{Text|10|#F580B1}}
  {{Cl|INPUT}} "Enter a numerical value to convert to binary(0 quits): ", decimal&
    {{Cl|INPUT}} {{Text|<nowiki>"Enter a numerical value to convert to binary(0 quits): "</nowiki>|#FFB100}}, decimal&
  {{Cl|CLS}}: {{Cl|LOCATE}} 10, 10  
    {{Cl|CLS}}: {{Cl|LOCATE}} {{Text|10|#F580B1}}, {{Text|10|#F580B1}}
  Decimal2Binary (decimal&)       'pass number by value using {{Cl|parenthesis}}
    {{Text|Decimal2Binary|#55FF55}} (decimal&) {{Text|<nowiki>'pass number by value using parenthesis</nowiki>|#919191}}
{{Cl|LOOP}} {{Cl|UNTIL}} decimal& = 0
{{Cl|DO...LOOP|LOOP UNTIL}} decimal& = {{Text|0|#F580B1}}
{{Cl|END}}
{{Cl|END}}


{{Cl|SUB}} Decimal2Binary(number&)
{{Cl|SUB}} {{Text|Decimal2Binary|#55FF55}} (number&)
IF number& = 0 THEN {{Cl|EXIT}} SUB
    {{Cl|IF}} number& = {{Text|0|#F580B1}} {{Cl|THEN}} {{Cl|EXIT SUB}}
DO
    {{Cl|DO}}
  remain% = {{Cl|ABS}}(number& {{Cl|MOD}} 2)   ' remainder is used to create binary number
        remain% = {{Cl|ABS}}(number& {{Cl|MOD}} {{Text|2|#F580B1}}) {{Text|<nowiki>' remainder is used to create binary number</nowiki>|#919191}}
  number& = number& \ 2         ' move up one exponent of 2 with integer division
        number& = number& \ {{Text|2|#F580B1}} {{Text|<nowiki>' move up one exponent of 2 with integer division</nowiki>|#919191}}
  Bin$ = {{Cl|LTRIM$}}({{Cl|STR$}}(remain%))   ' make remainder a string number
        BinStr$ = {{Cl|LTRIM$}}({{Cl|STR$}}(remain%)) {{Text|<nowiki>' make remainder a string number</nowiki>|#919191}}
  Binary$ = Bin$ + Binary$       ' add remainder to binary number
        Binary$ = BinStr$ + Binary$ {{Text|<nowiki>' add remainder to binary number</nowiki>|#919191}}
LOOP UNTIL number& = 0
    {{Cl|DO...LOOP|LOOP UNTIL}} number& = {{Text|0|#F580B1}}
PRINT "Binary number = " + Binary$   'binary result
    {{Cl|PRINT}} {{Text|<nowiki>"Binary number = "</nowiki>|#FFB100}} + Binary$ {{Text|<nowiki>'binary result</nowiki>|#919191}}
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' Displays Binary bits on as 1. Remainder can only be 1 or 0 in each loop. Can return all positive values correctly. The decimal parameter could come from a register return value read by [[INP]](address) and 1 would indicate the pins or bits on.
:''Explanation:'' Displays Binary bits on as 1. Remainder can only be 1 or 0 in each loop. Can return all positive values correctly. The decimal parameter could come from a register return value read by [[INP]](address) and 1 would indicate the pins or bits on.
Line 65: Line 65:
:''Example 2:'' Turning flip-flop [[BIT]] flags on or off and determining the bits on in a [[BYTE]] of program data.
:''Example 2:'' Turning flip-flop [[BIT]] flags on or off and determining the bits on in a [[BYTE]] of program data.


{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|_DEFINE}} B {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}}   'unsigned allows byte values from 128 to 255
{{Cl|_DEFINE}} B {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}} {{Text|<nowiki>'unsigned allows byte values from 128 to 255</nowiki>|#919191}}


{{Cl|PRINT}} "Hit number keys 1 to 8 to turn flags ON or OFF"
{{Cl|PRINT}} {{Text|<nowiki>"Hit number keys 1 to 8 to turn flags ON or OFF"</nowiki>|#FFB100}}


DO
{{Cl|DO}}
  a$ = {{Cl|INKEY$}} 'get bit numbers 1 through 8 only
    a$ = {{Cl|INKEY$}} {{Text|<nowiki>'get bit numbers 1 through 8 only</nowiki>|#919191}}
  k = {{Cl|VAL}}(a$)
    k = {{Cl|VAL}}(a$)
  {{Cl|IF...THEN|IF}} k > 0 {{Cl|AND (boolean)|AND}} k < 9 {{Cl|THEN}} 'test for switch on or off
    {{Cl|IF}} k > {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} k < {{Text|9|#F580B1}} {{Cl|THEN}} {{Text|<nowiki>'test for switch on or off</nowiki>|#919191}}
    bitval = 2 ^ (k - 1)  
        bitval = {{Text|2|#F580B1}} ^ (k - {{Text|1|#F580B1}})
    {{Cl|IF...THEN|IF}} (byte {{Cl|AND}} bitval) = 0 {{Cl|THEN}} byte = byte + bitval {{Cl|ELSE}} byte = byte - bitval                
        {{Cl|IF}} (byte {{Cl|AND (boolean)|AND}} bitval) = {{Text|0|#F580B1}} {{Cl|THEN}} byte = byte + bitval {{Cl|ELSE}} byte = byte - bitval
    {{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} "Switches on:";
        {{Cl|LOCATE}} {{Text|10|#F580B1}}, {{Text|10|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Switches on:"</nowiki>|#FFB100}};
    {{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 7                     'maximum byte value is 255
        {{Cl|FOR}} i = {{Text|0|#F580B1}} {{Cl|TO}} {{Text|7|#F580B1}} {{Text|<nowiki>'maximum byte value is 255</nowiki>|#919191}}
      {{Cl|IF...THEN|IF}} (byte {{Cl|AND}} 2 ^ i) {{Cl|THEN}} {{Cl|PRINT}} i + 1;
            {{Cl|IF}} (byte {{Cl|AND (boolean)|AND}} {{Text|2|#F580B1}} ^ i) {{Cl|THEN}} {{Cl|PRINT}} i + {{Text|1|#F580B1}};
    {{Cl|NEXT}}
        {{Cl|NEXT}}
    {{Cl|PRINT}} "Byte ="; byte; {{Cl|SPACE$}}(4)
        {{Cl|PRINT}} {{Text|<nowiki>"Byte ="</nowiki>|#FFB100}}; byte; {{Cl|SPACE$}}({{Text|4|#F580B1}})
  {{Cl|END IF}}
    {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|_KEYDOWN}}(27)
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|_KEYDOWN}}({{Text|27|#F580B1}})
{{Cl|SLEEP}}
{{Cl|SLEEP}}
{{Cl|SYSTEM}} '' ''
{{Cl|SYSTEM}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
:''Explanation:'' The byte value is checked to see if the switch is already on. If it is not on, then the bit value is turned on by adding that value to the byte value. If it is on, that value is subtracted. The [[FOR...NEXT]] loop reads each bit and displays the switches on.
:''Explanation:'' The byte value is checked to see if the switch is already on. If it is not on, then the bit value is turned on by adding that value to the byte value. If it is on, that value is subtracted. The [[FOR...NEXT]] loop reads each bit and displays the switches on.




 
{{PageSeeAlso}}
''See also:''
* [[_BIT]], [[&B]], [[_BYTE]]
* [[_BIT]], [[&B]], [[_BYTE]]
* [[_SHL]], [[_SHR]]
* [[_SHL]], [[_SHR]]
Line 100: Line 99:




 
{{PageReferences}}
{{PageNavigation}}

Latest revision as of 12:25, 19 November 2024

Binary is the base 2 numbering system. It is used by computers because the computer consists of switches that are either on or off. The primary purpose of reading bit values is to translate what was sent by a port or register read.


  • Base 2 has numerical values of 0 for off or 1 for on. There is no QBasic function to return the binary values.
  • A computer register has 8 bit switches for one byte of data. Each register can return values from 0 to 255 just like ASCII.
  • Each bit can be read by using an exponent of 2 from 0 to 128(2 ^ 0 to 2 ^ 7) utilizing the AND numerical operator.
  • Bit numbers and parallel port pins are designated as exponents of 2 also for clarity.
  • The first bit(2 ^ 0) of one byte is called the least significant bit(LSB).
  • The highest bit(2 ^ 7) of one byte is called the most significant bit(MSB).
  • A four bit reading or writing mode is called a nibble mode.


Decimal Bit values returned when a bit is on:
                                    Exponent of 2 and Bit #  7 6 5 4 3 2 1 0 

                       Bit 0 = 2 ^ 0 = 1 decimal    binary = 0 0 0 0 0 0 0 1
                       Bit 1 = 2 ^ 1 = 2 decimal    binary = 0 0 0 0 0 0 1 0
                       Bit 2 = 2 ^ 2 = 4 decimal    binary = 0 0 0 0 0 1 0 0
                       Bit 3 = 2 ^ 3 = 8 decimal    binary = 0 0 0 0 1 0 0 0
                       Bit 4 = 2 ^ 4 = 16 decimal   binary = 0 0 0 1 0 0 0 0
                       Bit 5 = 2 ^ 5 = 32 decimal   binary = 0 0 1 0 0 0 0 0
                       Bit 6 = 2 ^ 6 = 64 decimal   binary = 0 1 0 0 0 0 0 0
                       Bit 7 = 2 ^ 7 = 128 decimal  binary = 1 0 0 0 0 0 0 0

                       All_Bits_On = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255
Explanation: The table above only displays the binary value of each bit when on. If a value of 255 was read, the binary number = 11111111 (all bits on). If only bits 2, 3 and 4 are on then the value would be 4 + 8 + 16 = 28 or 00011100.


Truth table of the BASIC Logical Operators:


               Table 4: The logical operations and its results.

       In this table, A and B are the Expressions to invert or combine.
              Both may be results of former Boolean evaluations.
  ┌────────────────────────────────────────────────────────────────────────┐
  │                           Logical Operations                           │
  ├───────┬───────┬───────┬─────────┬────────┬─────────┬─────────┬─────────┤
  │   ABNOT BA AND BA OR BA XOR BA EQV BA IMP B │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ truetrue  │ false │  true   │ true   │  false  │  true   │  true   │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ truefalse │ true  │  false  │ true   │  true   │  false  │  false  │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ falsetrue  │ false │  false  │ true   │  true   │  false  │  true   │
  ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤
  │ falsefalse │ true  │  false  │ false  │  false  │  true   │  true   │
  └───────┴───────┴───────┴─────────┴────────┴─────────┴─────────┴─────────┘
   Note: In most BASIC languages incl. QB64 these are bitwise operations,
         hence the logic is performed for each corresponding bit in both
         operators, where true or false indicates whether a bit is set or
         not set. The outcome of each bit is then placed into the respective
         position to build the bit pattern of the final result value.

   As all Relational Operations return negative one (-1, all bits set) for
    true and zero (0, no bits set) for false, this allows us to use these
    bitwise logical operations to invert or combine any relational checks,
    as the outcome is the same for each bit and so always results into a
            true (-1) or false (0) again for further evaluations.


Explanation: The Logical Operators can be used to change bit values of a byte when comparing them to another byte value. AND can determine if each bit is on using a comparison with exponents of 2 from 0 to 7. OR can turn on a bit only when at least one bit is on in a comparison. It cannot turn off a bit! XOR can switch bits on if only ONE of the values has a bit on. Not both! These 3 logical operators are often used to work with register or port readings to turn bits on or off.


Example 1: Converting decimal values to a string binary value. Use LOCATE before the SUB call to locate the PRINT.
DO
    LOCATE 2, 10
    INPUT "Enter a numerical value to convert to binary(0 quits): ", decimal&
    CLS: LOCATE 10, 10
    Decimal2Binary (decimal&) 'pass number by value using parenthesis
LOOP UNTIL decimal& = 0
END

SUB Decimal2Binary (number&)
    IF number& = 0 THEN EXIT SUB
    DO
        remain% = ABS(number& MOD 2) ' remainder is used to create binary number
        number& = number& \ 2 ' move up one exponent of 2 with integer division
        BinStr$ = LTRIM$(STR$(remain%)) ' make remainder a string number
        Binary$ = BinStr$ + Binary$ ' add remainder to binary number
    LOOP UNTIL number& = 0
    PRINT "Binary number = " + Binary$ 'binary result
END SUB
Explanation: Displays Binary bits on as 1. Remainder can only be 1 or 0 in each loop. Can return all positive values correctly. The decimal parameter could come from a register return value read by INP(address) and 1 would indicate the pins or bits on.


Example 2: Turning flip-flop BIT flags on or off and determining the bits on in a BYTE of program data.
_DEFINE B AS _UNSIGNED _BYTE  'unsigned allows byte values from 128 to 255

PRINT "Hit number keys 1 to 8 to turn flags ON or OFF"

DO
    a$ = INKEY$ 'get bit numbers 1 through 8 only
    k = VAL(a$)
    IF k > 0 AND k < 9 THEN 'test for switch on or off
        bitval = 2 ^ (k - 1)
        IF (byte AND bitval) = 0 THEN byte = byte + bitval ELSE byte = byte - bitval
        LOCATE 10, 10: PRINT "Switches on:";
        FOR i = 0 TO 7 'maximum byte value is 255
            IF (byte AND 2 ^ i) THEN PRINT i + 1;
        NEXT
        PRINT "Byte ="; byte; SPACE$(4)
    END IF
LOOP UNTIL _KEYDOWN(27)
SLEEP
SYSTEM
Code by Ted Weissgerber
Explanation: The byte value is checked to see if the switch is already on. If it is not on, then the bit value is turned on by adding that value to the byte value. If it is on, that value is subtracted. The FOR...NEXT loop reads each bit and displays the switches on.


See also


QB64 Programming References

Wiki Pages
Main Page with Articles and Tutorials
QB64 specific keywords (alphabetical)
Original QBasic keywords (alphabetical)
QB64 OpenGL keywords (alphabetical)
Keywords by Usage
Got a question about something?
Frequently Asked Questions about QB64
QB64 Phoenix Edition Community Forum
Links to other QBasic Sites:
Pete's QBasic Forum
Pete's QBasic Downloads