### Denoting values in base 2, 8 and 16 in C (and in Java)

• Denoting numbers in the Binary number system

• Bit patterns is a royal pain to write using decimal representation

It is a lot easier to write bit patterns using the Binary Number System

• C provides you with different number systems to write bit patterns with more ease !

• Denoting binary numbers in C (with the prefix 0b):

 A number that starts with the prefix 0b (ZERO b) is considered to use the Binary Number System as representation

Example:

 ``` 0bxxxxxxxx = the binary number xxxxxxxx Example: 0b00001111 = 15 decimal ```

• Example:

 ``` int main( int argc, char* argv[] ) { char a = 0; /* 0 = 00000000 */ /* Bit pos: 76543210 */ a = a | 0b00000100; /* Now: a = 00000100 */ printf( "a = %d\n", a ); a = a | 0b00010000; /* Now: a = 00010100 */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc bit-op2a.c To run:          ./a.out

• Denoting numbers in the Octal number system

• Disadvantage of the Binary Number System:

 The number of digits needed will grown very fast !!!

We can use other related number system to express bit patterns in more compact form

• Octal numbers uses base 8 is the representation

Example:

 ``` Representation Value represented --------------------------------------------------- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10 8 11 9 12 10 ... ... 23 2*8 + 3 = 19 ```

• The Octal number system (8) is related to the Binary number system (2) because:

 ``` 8 = 23 ```

Because the FONT color="blue">Octal number system (8) is related to the Binary number system (2), converting values between these systems is easy (we have discussed this at the start of this course !!!)

• Denoting Octal numbers in C (with the prefix 0):

 A number that starts with the prefix 0 (ZERO) is considered to use the Octal Number System as representation

Example:

 ``` 0xxxxxxxx = the Octal number xxxxxxxx Example: 011 = 9 decimal (8 + 1) ```

• Example:

 ``` int main( int argc, char* argv[] ) { char a = 011; /* 11 Octal = 8 + 1 = 9 decimal */ printf( "a = %d\n", a ); a = 0101; /* 101 Octal = 64 + 1 = 65 decimal */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc octal1.c To run:          ./a.out

• Denoting numbers in the Hexadecimal number system

• Hexadecimal numbers uses base 16 is the representation

Example:

 ``` Representation Value represented --------------------------------------------------- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A 10 B 11 C 12 D 13 E 14 F 15 10 16 11 17 12 18 ... ... 23 2*16 + 3 = 35 ```

• Denoting Hexadecimal numbers in C (with the prefix 0x):

 A number that starts with the prefix 0x (ZERO x) is considered to use the Hexadecimal Number System as representation

Example:

 ``` 0xaaaaa = the Hexadecimal number aaaaa Example: 0x1A = 26 decimal (16 + 10) ```

• Example:

 ``` int main( int argc, char* argv[] ) { int a = 0x1A; /* 1A Hexadecimal = 16 + 10 = 26 decimal */ printf( "a = %d\n", a ); a = 0x10A; /* 101 Hexadecimal = 256 + 10 = 266 decimal */ printf( "a = %d\n", a ); } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc hexadec1.c To run:          ./a.out