It is a lot easier to write bit patterns using the Binary Number System
|
Example:
0bxxxxxxxx = the binary number xxxxxxxx
Example:
0b00001111 = 15 decimal
|
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 );
}
|
How to run the program:
|
|
We can use other related number system to express bit patterns in more compact form
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
|
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 !!!)
|
Example:
0xxxxxxxx = the Octal number xxxxxxxx
Example:
011 = 9 decimal (8 + 1)
|
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 );
}
|
How to run the program:
|
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
|
|
Example:
0xaaaaa = the Hexadecimal number aaaaa
Example:
0x1A = 26 decimal (16 + 10)
|
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 );
}
|
How to run the program:
|