Base Comparisons: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{WhiteStart}} <tt> '''Comparing the Base Numbering Systems'''</tt> '''<tt>Decimal (base 10) Binary (base 2) Hexadecimal (base 16) Octal (base 8)</tt>''' 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3...")
 
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{WhiteStart}}
{{FixedStart}}
                        '''Comparing the Base Numbering Systems'''


                      <tt> '''Comparing the Base Numbering Systems'''</tt>
     '''Decimal (base 10)    Binary (base 2)    Hexadecimal (base 16)    Octal (base 8)'''
 
     '''<tt>Decimal (base 10)    Binary (base 2)    Hexadecimal (base 16)    Octal (base 8)</tt>'''


           0                  0000                  0                    0
           0                  0000                  0                    0
Line 22: Line 21:
         15  -------------  1111 <--- Match --->  F  ----------------  17 -- max 2
         15  -------------  1111 <--- Match --->  F  ----------------  17 -- max 2
         16                10000                10                    20
         16                10000                10                    20
       
 
       When the Decimal value is 15, the other 2 base systems are all maxed out!
       When the Decimal value is 15, the other 2 base systems are all maxed out!
       The Binary values can be compared to all of the HEX value digit values so
       The Binary values can be compared to all of the HEX value digit values so
Line 28: Line 27:
       value to Binary just add the 4 binary digits for each HEX digit place so:
       value to Binary just add the 4 binary digits for each HEX digit place so:


                         F      A      C      E  
                         F      A      C      E
               &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101
               &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101


Line 34: Line 33:
       sections of four digits starting from the right(LSB) end. If one has less
       sections of four digits starting from the right(LSB) end. If one has less
       than 4 digits on the left end you could add the leading zeros like below:
       than 4 digits on the left end you could add the leading zeros like below:
            &B101011100010001001 = 0010 1011 1000 1000 1001 
                      hexadecimal =  2  + B  + 8 +  8  + 9 = &H2B889
    See the Decimal to Binary conversion function that uses '''[[HEX$]]''' on the '''[[&H]]''' page.
{{WhiteEnd}}


            &B101011100010001001 = 0010 1011 1000 1000 1001
                      hexadecimal =  2  + B  + 8 +  8  + 9 = &H2B889


''Example:'' Outputs all of the decimal, hexadecimal and octal digits:
    See the Decimal to Binary conversion function that uses '''[[HEX$]]''' on the '''[[&H]]''' page,
    but take it for education only. In QB64-PE just use the new '''[[_BIN$]]''' function.
{{FixedEnd}}




{{PageExamples}}
;Example:Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
{{CodeStart}}
{{CodeStart}}
          PRINT " Decimal | Hexadecimal | Octal "
tabletop$ = " Decimal | Hexadecimal | Octal | Binary "
          PRINT "---------+-------------+-------"
tablesep$ = "---------+-------------+-------+--------"
tableout$ = "  \ \    |      \\    |  \\  |  \  \  " 'the PRINT USING template


FOR n% = 0 TO 15
{{Cl|LOCATE}} 2, 10: {{Cl|PRINT}} tabletop$
     PRINT USING "    ##  |         \\ |    \\ " ; n% ; HEX$(n%) ; OCT$(n%)
{{Cl|LOCATE}} 3, 10: {{Cl|PRINT}} tablesep$
NEXT n%
{{Cl|FOR...NEXT|FOR}} n% = 0 {{Cl|TO}} 15
     {{Cl|LOCATE}} 4 + n%, 10: {{Cl|PRINT USING}} tableout$; {{Cl|STR$}}(n%); {{Cl|HEX$}}(n%); {{Cl|OCT$}}(n%); {{Cl|_BIN$}}(n%)
{{Cl|NEXT}} n%
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}<nowiki> Decimal | Hexadecimal | Octal
;Note:Although the decimal numbers 0-15 have a maximum width of 2 digits only, an extra space in the ''tableout$'' template is needed when using the (fixed width string) slash output format, as [[STR$]] values contain a leading sign placeholder space.
---------+-------------+-------
{{OutputStart}}
      0 |         |    0
          Decimal | Hexadecimal | Octal | Binary
      1 |         |    1
        ---------+-------------+-------+--------
      2 |         2 |   2
            0     |      0      |   0   | 0
      3 |         3 |   3
            1     |      1      |   1   | 1
      4 |         4 |   4
            2     |     2     |   2   |  10
      5 |         5 |   5
            3     |     3     |   3   |  11
      6 |         6 |   6
            4     |     4     |   4   |  100
      7 |         7 |   7
            5     |     5     |   5   |  101
      8 |         8  |   10
            6     |     6     |   6   |  110
      9 |         9  |   11
            7     |     7     |   7   |  111
    10 |         A  |   12
            8     |     8     |  10 | 1000
    11 |         B  |   13
            9     |     9     |  11 | 1001
    12 |         C  |   14
            10   |     A     |  12 | 1010
    13 |         D  |   15
            11   |     B     |  13 | 1011
    14 |         E  |   16
            12   |     C     |  14 | 1100
    15 |         F  |   17
            13   |     D     |  15 | 1101
</nowiki>{{OutputEnd}}
            14   |     E     |  16 | 1110
            15   |     F     |  17 | 1111
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* {{KW|OCT$}}, {{KW|VAL}}
* [[_BIN$]], [[HEX$]], [[OCT$]], [[STR$]]
* {{KW|&H}} (hexadecimal), {{KW|&O}} (octal), {{KW|&B}} (binary) 
* [[&B]] (binary), [[&H]] (hexadecimal), [[&O]] (octal), [[VAL]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 23:33, 10 January 2023

                        Comparing the Base Numbering Systems

     Decimal (base 10)    Binary (base 2)    Hexadecimal (base 16)    Octal (base 8)

          0                  0000                  0                     0
          1                  0001                  1                     1
          2                  0010                  2                     2
          3                  0011                  3                     3
          4                  0100                  4                     4
          5                  0101                  5                     5
          6                  0110                  6                     6
          7                  0111                  7                     7 -- maxed
          8                  1000                  8                    10
  maxed-- 9                  1001                  9                    11
         10                  1010                  A                    12
         11                  1011                  B                    13
         12                  1100                  C                    14
         13                  1101                  D                    15
         14                  1110                  E                    16
         15  -------------   1111 <--- Match --->  F  ----------------  17 -- max 2
         16                 10000                 10                    20

      When the Decimal value is 15, the other 2 base systems are all maxed out!
      The Binary values can be compared to all of the HEX value digit values so
      it is possible to convert between the two quite easily. To convert a HEX
      value to Binary just add the 4 binary digits for each HEX digit place so:

                        F      A      C      E
              &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101

      To convert a Binary value to HEX you just need to divide the number into
      sections of four digits starting from the right(LSB) end. If one has less
      than 4 digits on the left end you could add the leading zeros like below:

             &B101011100010001001 = 0010 1011 1000 1000 1001
                       hexadecimal =  2  + B  + 8 +  8  + 9 = &H2B889

    See the Decimal to Binary conversion function that uses HEX$ on the &H page,
    but take it for education only. In QB64-PE just use the new _BIN$ function.


Examples

Example
Comparing decimal, hexadecimal, octal and binary string values from 0 to 15.
tabletop$ = " Decimal | Hexadecimal | Octal | Binary "
tablesep$ = "---------+-------------+-------+--------"
tableout$ = "  \ \    |      \\     |   \\  |  \  \  " 'the PRINT USING template

LOCATE 2, 10: PRINT tabletop$
LOCATE 3, 10: PRINT tablesep$
FOR n% = 0 TO 15
    LOCATE 4 + n%, 10: PRINT USING tableout$; STR$(n%); HEX$(n%); OCT$(n%); _BIN$(n%)
NEXT n%
Note
Although the decimal numbers 0-15 have a maximum width of 2 digits only, an extra space in the tableout$ template is needed when using the (fixed width string) slash output format, as STR$ values contain a leading sign placeholder space.
          Decimal | Hexadecimal | Octal | Binary
         ---------+-------------+-------+--------
            0     |      0      |   0   |  0
            1     |      1      |   1   |  1
            2     |      2      |   2   |  10
            3     |      3      |   3   |  11
            4     |      4      |   4   |  100
            5     |      5      |   5   |  101
            6     |      6      |   6   |  110
            7     |      7      |   7   |  111
            8     |      8      |   10  |  1000
            9     |      9      |   11  |  1001
            10    |      A      |   12  |  1010
            11    |      B      |   13  |  1011
            12    |      C      |   14  |  1100
            13    |      D      |   15  |  1101
            14    |      E      |   16  |  1110
            15    |      F      |   17  |  1111


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link