Recent news items :

  1. There is a sample final exam.

    Posted: Tuesday, December 10, 2002

 

Binary numbers, bit operations, etc

Tuesday, November 19, 2002


Previous page | Next page


Hexadecimal (“hex”) notation

Hex numbers are just a convenient way to abbreviate long bitstrings by writing four bits as a single hex (base-16) digit:
    0000   0
    0001   1
    0010   2
    0011   3
    0100   4
    0101   5
    0110   6
    0111   7
    1000   8
    1001   9
    1010   A
    1011   B
    1100   C
    1101   D
    1110   E
    1111   F
In C++ hex numbers are written with a prefix 0x. The demo contains examples. 0xFF thus means decimal 255; 0x100 means decimal 256; two hex digits make up one byte (= eight bits).

Negative integers

Another aspect of binary numbers is how negative numbers are represented. Here is an example pretending that int-s only use 4 bits (not 32), showing the decimal equivalents of all sixteen 4-bit patterns:
           int            unsigned int

            0      0000       0
            1      0001       1
            2      0010       2
            3      0011       3
            4      0100       4
            5      0101       5
            6      0110       6
            7      0111       7
           -8      1000       8
           -7      1001       9
           -6      1010      10
           -5      1011      11
           -4      1100      12
           -3      1101      13
           -2      1110      14
           -1      1111      15
This representation of negative integers is called “2's complement.” Note how you figure out the representation of negative numbers, e.g., -5:
start with the
   positive number, 5:  0101

        flip all bits:  1010
     ...  and add one:     1
                        ----
                   -5:  1011
Strange as it may look, this representation has a very useful property: you can add two bitstrings without having to know whether they are meant to represent signed or unsigned int-s:
      decimal        bit     decimal
      equivalent   pattern   equivalent
      if int                 if unsigned int

            2        0010       2
           -5        1011      11
          ---        ----     ---
           -3        1101      13
Thus there is no need for two sets of addition circuitry.

You also can see why your friendly compiler warns you when you mix (signed) int-s with unsigned int-s: in a 4-bit system, an unsigned 13 would be equal to a (signed) -3! Here is a demo illustrating this matter for 32-bit numbers.

Another matter that this same demo illustrates is that C++ provides operations on the bits such as the >> for shifting to the right (yes, the same symbol that is used for the input operator) and the & for “bitwise and” used in the printing of the bits:

    // showing the actual bits
    cout << "unsigned int = ";
    for (int b = 31; b >= 0; b--)
        cout << ((ui >> b) & 1);


Previous page | Next page | Back to top

3:40 PM, Thursday, December 12, 2002