G.2 Relationship between Binary, Octal, and Hexadecimal Numbers


From Table G.1 we see that 3 bits are needed to represent any octal digit, and 4 bits to are needed to represent any hexadecimal digit. We can use this fact to convert between binary, octal, and hexadecimal systems, as shown in Figure G.1.

Figure G.1. Converting between Binary, Octal, and Hexadecimal

graphics/gfig01.gif

The procedure for converting an octal to a binary is shown by the arrow marked (a). We can prove that replacing each octal digit by its 3-bit equivalent binary value gives the right result:

173 8

= 1 x8 2

+ 7 x8 1

+ 3 x8

 

= 1 x(2 3 ) 2

+ 7 x(2 3 ) 1

+ 3 x(2 3 )

 

= 1 x2 6

+ 7 x2 3

+ 3

 

= (001 2 )x2 6

+ (111 2 )x2 3

+ (011 2 )

 

= (0x2 2 +0x2 1 +1x2 )x2 6

+ (1x2 2 +1x2 1 +1x2 )x2 3

+ (0x2 2 +1x2 1 +1x2 )

 

= 1 x2 6

+ 1 x2 5 + 1 x2 4 + 1 x2 3

+ x2 2 + 1 x2 1 + 1 x2

 

= 1 x2 6 + 1 x2 5 + 1 x2 4 + 1 x2 3 + x2 2 + 1 x2 1 + 1 x2

 

= 1111011 2

   

Analogously, we can convert a hexadecimal number to its equivalent binary number by replacing each digit in the hexadecimal number by its 4-bit equivalent binary value, as shown by the arrow marked (b).

To convert a binary to it octal equivalent, we reverse the procedure outlined earlier (see arrow marked (c) in Figure G.1). The bits in the binary number are grouped into 3-bit groups from right to left. Each such group is replaced by its equivalent octal digit. This corresponds to reversing the computation shown above. Analogously, we can convert a binary to a hexadecimal number by replacing each 4-bit group by its equivalent hex digit (see arrow marked (d) in Figure G.1).



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net