Visual Basic

Number Systems
of
Decimal, Octal, Hexadecimal and Binary

Decimal

All of us we are accustomed since young to use decimal numbers to express quantities. This nomenclature that seems so logical to us may not seems it to a Roman of the classic Rome. For them each symbol that they put to express a number always represented the same value:

I      1
II     2
III    3
IV     4
V      5
VI     6
VII    7
VIII   8
IX     9
X     10
XI    11
If one pays attention all the I signs always represents the value 1 (one) wherever they be placed, like the V sign always represents our 5 (five). Nevertheless that does not take place in our decimal system. When we write the decimal symbol 1 we are not always talking about value 1 (I in Roman numbers). For example:
  1    I
 10    X
100    C
In these cases our symbol 1 does not always has a value of 1 (I in Roman numbers). For example, in the second case, the symbol 1 represents the value 10 (ten, X in Roman) and in the third one, 1 represents the value 100 (one hundred, C).

Another example:

275
is not equivalent to 2+7+5, rather could be decomposed as 200+70+5:
 200
  70
+  5
-----
 275
therefore, the first sign 2 is equivalent to 200 (2 x 100), the second sign, 7 is equivalent to 70 (7 x 10) whereas the last sign corresponds to value 5 (5 x 1).

All the previous can be mathematically represented in a very simple way. For example, to represent the value 182736 we can assume that each digit is the product of itself multiplied by 10 powered to its place as exponent, beginning from the right with 100, following with 101, 102, and so on:

 

Octal numbers (radix 8)

Like our "normal" numbers are radix 10 because we have 10 different digits (from the 0 to the 9):
0123456789
the octals numbers include only the signs from the 0 to the 7:
01234567
and, therefore, its mathematical radix is 8. In C++ octal numbers have the peculiarity that they always they begin by a 0 digit. Let us see how we would write the first numbers in octal:
octal  decimal
-----  -------
  0       0   (zero)
  1       1   (one)
  2       2   (two)
  3       3   (three)
  4       4   (four)
  5       5   (five)
  6       6   (six)
  7       7   (seven)
 10       8   (eight)
 11       9   (nine)
 12      10   (ten)
 13      11   (eleven)
 14      12   (twelve)
 15      13   (thirteen)
 16      14   (fourteen)
 17      15   (fifteen)
 20      16   (sixteen)
 21      17   (seventeen)
Thus, for example, number 17 (seventeen, or XVII in Romans) it is expressed 021 as an octal number.

We can apply the scheme that we saw previously with the decimal numbers to the octal numbers simply by considering that its radix is 8. For example, taking the octal number 071263:

therefore the octal numbers 071263 it is expressed as 29363 in decimal numbers.

 

Hexadecimal numbers (radix 16)

Like decimal numbers have 10 different digits to be represented (0123456789) and octal numbers have 8 (01234567), hexadecimal numbers have 16: numbers from 0 to 9 and letters A, B, C, D, E and F that together they serve us to represent the 16 different symbols that we need:
hexadecimal  decimal
-----------  -------
      0         0   (zero)
      1         1   (one)
      2         2   (two)
      3         3   (three)
      4         4   (four)
      5         5   (five)
      6         6   (six)
      7         7   (seven)
      8         8   (eight)
      9         9   (nine)
      A        10   (ten)
      B        11   (eleven)
      C        12   (twelve)
      D        13   (thirteen)
      E        14   (fourteen)
      F        15   (fifteen)
     10        16   (sixteen)
     11        17   (seventeen)
Once again we can use the same method to translate a number from a base to another one:

 

Binary representations

Octal and hexadecimal numbers have a considerable advantage over our decimal numbers in the world of bits, and is that their bases (8 and 16) are perfect multiples of 2 (23 and 24) which allows us to make easier conversions to binary than with decimal numbers (whose base is 2x5). For example, suppose that we want to pass the following binary sequence to numbers of other bases:
110011111010010100
In order to pass it to decimal we would need to conduct a mathematical operation similar to the one we have used previously to convert from hexadecimal or octal, which would give us the decimal number 212628.

Nevertheless to pass this sequence to octal it will only take to us some seconds and we can do it just seeing it: Since 8 is 23, we will separate the binary value in groups of 3 numbers:

110 011 111 010 010 100
and now we just have to translate to octal numberal radix each group separately:
110 011 111 010 010 100
 6   3   7   2   2   4
giving the number 637224 as result. This same process can be inversely performed to pass from octal to binary.

In order to conduct the operation with hexadecimal numbers we only have to perform the same process but separating the binary value in groups of 4 numbers (16 = 24):

0011 0011 1110 1001 0100
  3    3    E    9    4
Therefore, the binary expression 110011111010010100 can be represented in C++ as 212628 (decimal), as 0637224 (octal) or as 0x33e94 (hexadecimal).

The hexadecimal code is specially interesting in computer science since nowadays computers are based on bytes composed of 8 binary bits and therefore each byte matches with the rank that 2 hexadecimal numbers can represent. For that reason is the most used type when representing values translated from binary.

 

Comparison Table

       Decimal   Octal   Hex    Binary     Value
       -------   -----   ---    ------     -----
         000      000    000   00000000      NUL    (Null char.)
         001      001    001   00000001      SOH    (Start of Header)
         002      002    002   00000010      STX    (Start of Text)
         003      003    003   00000011      ETX    (End of Text)
         004      004    004   00000100      EOT    (End of Transmission)
         005      005    005   00000101      ENQ    (Enquiry)
         006      006    006   00000110      ACK    (Acknowledgment)
         007      007    007   00000111      BEL    (Bell)
         008      010    008   00001000       BS    (Backspace)
         009      011    009   00001001       HT    (Horizontal Tab)
         010      012    00A   00001010       LF    (Line Feed)
         011      013    00B   00001011       VT    (Vertical Tab)
         012      014    00C   00001100       FF    (Form Feed)
         013      015    00D   00001101       CR    (Carriage Return)
         014      016    00E   00001110       SO    (Shift Out)
         015      017    00F   00001111       SI    (Shift In)
         016      020    010   00010000      DLE    (Data Link Escape)
         017      021    011   00010001      DC1 (XON) (Device Control 1)
         018      022    012   00010010      DC2       (Device Control 2)
         019      023    013   00010011      DC3 (XOFF)(Device Control 3)
         020      024    014   00010100      DC4       (Device Control 4)
         021      025    015   00010101      NAK    (Negative Acknowledgement)
         022      026    016   00010110      SYN    (Synchronous Idle)
         023      027    017   00010111      ETB    (End of Trans. Block)
         024      030    018   00011000      CAN    (Cancel)
         025      031    019   00011001       EM    (End of Medium)
         026      032    01A   00011010      SUB    (Substitute)
         027      033    01B   00011011      ESC    (Escape)
         028      034    01C   00011100       FS    (File Separator)
         029      035    01D   00011101       GS    (Group Separator)
         030      036    01E   00011110       RS    (Request to Send)(Record Separator)
         031      037    01F   00011111       US    (Unit Separator)
         032      040    020   00100000       SP    (Space)
         033      041    021   00100001        !
         034      042    022   00100010        "
         035      043    023   00100011        #
         036      044    024   00100100        $
         037      045    025   00100101        %
         038      046    026   00100110        &
         039      047    027   00100111        '
         040      050    028   00101000        (
         041      051    029   00101001        )
         042      052    02A   00101010        *
         043      053    02B   00101011        +
         044      054    02C   00101100        ,
         045      055    02D   00101101        -
         046      056    02E   00101110        .
         047      057    02F   00101111        /
         048      060    030   00110000        0
         049      061    031   00110001        1
         050      062    032   00110010        2
         051      063    033   00110011        3
         052      064    034   00110100        4
         053      065    035   00110101        5
         054      066    036   00110110        6
         055      067    037   00110111        7
         056      070    038   00111000        8
         057      071    039   00111001        9
         058      072    03A   00111010        :
         059      073    03B   00111011        ;
         060      074    03C   00111100        <
         061      075    03D   00111101        =
         062      076    03E   00111110        >
         063      077    03F   00111111        ?
         064      100    040   01000000        @
         065      101    041   01000001        A
         066      102    042   01000010        B
         067      103    043   01000011        C
         068      104    044   01000100        D
         069      105    045   01000101        E
         070      106    046   01000110        F
         071      107    047   01000111        G
         072      110    048   01001000        H
         073      111    049   01001001        I
         074      112    04A   01001010        J
         075      113    04B   01001011        K
         076      114    04C   01001100        L
         077      115    04D   01001101        M
         078      116    04E   01001110        N
         079      117    04F   01001111        O
         080      120    050   01010000        P
         081      121    051   01010001        Q
         082      122    052   01010010        R
         083      123    053   01010011        S
         084      124    054   01010100        T
         085      125    055   01010101        U
         086      126    056   01010110        V
         087      127    057   01010111        W
         088      130    058   01011000        X
         089      131    059   01011001        Y
         090      132    05A   01011010        Z
         091      133    05B   01011011        [
         092      134    05C   01011100        \
         093      135    05D   01011101        ]
         094      136    05E   01011110        ^
         095      137    05F   01011111        _
         096      140    060   01100000        `
         097      141    061   01100001        a
         098      142    062   01100010        b
         099      143    063   01100011        c
         100      144    064   01100100        d
         101      145    065   01100101        e
         102      146    066   01100110        f
         103      147    067   01100111        g
         104      150    068   01101000        h
         105      151    069   01101001        i
         106      152    06A   01101010        j
         107      153    06B   01101011        k
         108      154    06C   01101100        l
         109      155    06D   01101101        m
         110      156    06E   01101110        n
         111      157    06F   01101111        o
         112      160    070   01110000        p
         113      161    071   01110001        q
         114      162    072   01110010        r
         115      163    073   01110011        s
         116      164    074   01110100        t
         117      165    075   01110101        u
         118      166    076   01110110        v
         119      167    077   01110111        w
         120      170    078   01111000        x
         121      171    079   01111001        y
         122      172    07A   01111010        z
         123      173    07B   01111011        {
         124      174    07C   01111100        |
         125      175    07D   01111101        }
         126      176    07E   01111110        ~
         127      177    07F   01111111      DEL