Denoting binary numbers in C (and in Java)

  • Decimal numbers:   when a number starts with a non-zero digit:

      • The compiler will assume that the number is a decimal (base 10) number

    Examples:

      1 one
      12twelve 

  • Binary numbers:   when a number starts with 0b:

      • The remaining digits are assumed to be a binary number

    Examples:

      0b1011    ≡   binary number 1011
      0b11011   ≡   binary number 11011

Example C program that set the bit at position k revisited

  • Revisiting the following C program that sets the bit #2 and then sets the bit #4:

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 2                 
       a = a |  4;          // 4 = 00000010
                            // Result:    00000100 
    
       // Set bit 4
       a = a | 16;          // 16 = 00010000
                            // Result:    00010100 
    }

  • We can write the bit patterns easier using the 0bxxxxxxxx notation (next slide)

DEMO: demo/C/set1/bit-op2.c

Example C program that set the bit at position k revisited

  • The same C program, but using binary number notations:

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 2                 
       a = a | 0b100;       // bin 00000100  (leading 0's ignored)
                            // Result:    00000100 
    
       // Set bit 4
       a = a | 0b00010000;  // bin  00010000  (leading 0's OK)
                            // Result:    00010100 
    }

DEMO: demo/C/set1/bit-op2a.c

There is one small problem:   binary numbers can grow very quickly in length....
(E.g.: 0b10000000000000000000000000000000 )

Review: octal and hexadecimal numbers

  • Octal numbers can be easily converted to binary numbers and vice versa by considering groups of 3 bits:

     Octal digit      Binary digits     Octal digit      Binary digits
     -----------      -------------     -----------      -------------     
         0       <-->      000              4       <-->      100
         1       <-->      001		5       <-->      101
         2       <-->      010		6       <-->      110
         3       <-->      011		7       <-->      111

  • Hexadecimal numbers can be easily converted to binary numbers and vice versa by considering groups of 4 bits:

     Hexa digit       Binary digits     Hexa digit       Binary digits
     -----------      -------------     -----------      -------------     
         0       <-->     0000              8       <-->     1000
         1       <-->     0001		9       <-->     1001
         2       <-->     0010	        A       <-->     1010
         3       <-->     0011	        B       <-->     1011
         4       <-->     0100              C       <-->     1100
         5       <-->     0101	        D       <-->     1101
         6       <-->     0110	        E       <-->     1110
         7       <-->     0111	        F       <-->     1111

Denoting octal numbers and hexadecial number in C (and in Java)

  • Octal numbers:   when a number starts with 0 followed by a digit:

      • The number is assumed to be a octal number

    Examples:

      0123    ≡   octal number 123
      077     ≡   octal number 77

  • Hexadecimal numbers:   when a number starts with 0x:

      • The remaining number is assumed to be a hexadecimal number

    Examples:

      0xAB    ≡   hexadec number AB
      0x77    ≡   hexadec number 77

Example C program that set the bit at position k revisited using octal numbers

  • The following C program that sets the bit #5 and then sets the bit #7 using 0x (binary) number notation

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 5                 
       a = a | 0b00100000;  // Binary 00100000 = Octal 040
     
       // Set bit 7
       a = a | 0b10000000;  // Binary 10000000 = Octal 200
                           
    }

Example C program that set the bit at position k revisited using octal numbers

  • The same program written with octal number notation is:
     

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 5                 
       a = a | 040;         // Binary 00100000 = Octal 040
     
       // Set bit 7
       a = a | 0200;        // Binary 10000000 = Octal 200
                           
    }

DEMO: demo/C/set1/bit-op2b.c

Example C program that set the bit at position k revisited using hexadec numbers

  • The same program written with hexadecimal number notation is:
     

    int main( int argc, char* argv[] )
    {
       char a = 0;		/* 0 = 00000000 */
    
       // Set bit 5                 
       a = a | 0x20;        // Binary 00100000 = Hex 20
     
       // Set bit 7
       a = a | 0x80;        // Binary 10000000 = Hex 80
                           
    }

DEMO: demo/C/set1/bit-op2c.c