








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to convert binary numbers to hexadecimal and vice versa. It covers the hexadecimal number system, its relationship to binary, and provides examples and problem solutions for conversions.
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









Consider the following set of 32 binary digits, written in blocks of four so that the example is not impossible to read. 0010 0110 0100 1100 1101 1001 1011 1111 How do we interpret this sequence of binary digits? Answer: The interpretation depends on the use made of the number. Where is this 32–bit binary number found? Instruction Register If in the IR, this number will be decoded as an instruction, probably with an address part. Address Register If in the MAR or another address register, this is a memory address. Data Register If in a general purpose (data) register, this is data. Possibly a 32–bit real number Possibly a 32–bit integer Possibly four 8–bit character codes.
But first, we present a number system that greatly facilitates writing long strings of binary numbers. This is the hexadecimal system. The hexadecimal system (base 16 = 2 4 ) has 16 digits, the normal ten decimal digits and the first six letters of the alphabet. Because hexadecimal numbers have base 2 4 , each hexadecimal digit represents four binary bits. Hexadecimal notation is a good way to write binary numbers. The translation table from hexadecimal to binary is as follows. 0 0000 4 0100 8 1000 C 1100 1 0001 5 0101 9 1001 D 1101 2 0010 6 0110 A 1010 E 1110 3 0011 7 0111 B 1011 F 1111 Consider the previous example 0010 0110 0100 1100 1101 1001 1011 1111 As a hexadecimal number it is 264CD9BF, better written as 0x264C D9BF. The “0x” is the standard C++ and Java prefix for a hexadecimal constant.
This course is built upon three number systems and conversions between them. Binary Base 2 Digit set = {0, 1} Decimal Base 10 Digit set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Hexadecimal Base 16 Digit set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} We shall discuss five of the six possible conversion algorithms. I don’t know a good algorithm for direct conversion from decimal to hexadecimal, so I always use binary as an intermediate point. Octal (base 8) notation is useful in certain applications, but we won’t study it. Other number systems, such as base 5 and base 7, are useless teaching devices.
Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F
Remember (or calculate) the needed powers of sixteen, in decimal form. 16 0 = 1 16 1 = 16 16 2 = 256 16 3 = 4096 16 4 = 65536, etc.
Conversion between hexadecimal and binary is easy because 16 = 2 4 . In my view, hexadecimal is just convenient “shorthand” for binary. Thus, four hex digits stand for 16 bits, 8 hex digits for 32 bits, etc. But 10 is not a power of 2, so we must use different methods. Conversion from Binary to Decimal This is based on standard positional notation. Convert each “position” to its decimal equivalent and add them up. Conversion from Decimal to Binary This is done with two distinct algorithms, one for the digits to the left of the decimal point (the whole number part) and one for digits to the right. At this point we ignore negative numbers.
Again, we continue to ignore negative numbers. Problem: Convert 23.375 to binary. We already know the answer. One solution. 23.375 = 16 + 4 + 2 + 1 + 0.25 + 0. = 1 2 4
+ 1 2 - = 10111. This solution is preferred by your instructor, but most students find it confusing and opt to use the method to be discussed next. Side point: Conversion of the above to hexadecimal involves grouping the bits by fours as follows: Left of decimal: by fours from the right Right of decimal: by fours from the left. Thus the number is 1 0111.011 = 0001 0111.0110 or 0x17. But 0x17.6 = 116 + 71 + 6/16 = 23 + 3/8 = 23.
This is done by repeated division, with the remainders forming the binary number. This set of remainders is read “bottom to top” Quotient Remainder 23/2 = 11 1 Thus decimal 23 = binary 10111 11/2 = 5 1 5/2 = 2 1 Remember to read the binary 2/2 = 1 0 number from bottom to top. 1/2 = 0 1 As expected, the number is 10111 Another example: 16 Quotient Remainder 16/2 = 8 0 8/2 = 4 0 4/2 = 2 0 Remember to read the binary 2/2 = 1 0 number from bottom to top. 1/2 = 0 1 The number is 10000 or 0x
Consider the decimal number 0.20. What is its binary representation? Number Product Binary 0.20 2 = 0.40 0 0.40 2 = 0.80 0 0.80 2 = 1.60 1 0.60 2 = 1.20 1 0.20 2 = 0.40 0 0.40 2 = 0.80 0 0.80 2 = 1.60 1 but we have seen this – see four lines above. So 0.20 decimal has binary representation .00 1100 1100 1100 …. Terminating and Non–Terminating Numbers A fraction has a terminating representation in base–K notation only if the number can be represented in the form J / (B K ) Thus the fraction 1/2 has a terminating decimal representation because it is 5 / ( 1 ). It can also be 50 / ( 2 ), etc. Also 1/4 = 25 / ( 2 ), 1/8 = 125/( 3 ).
What about a decimal representation for 1/3? If we can generate a terminating decimal representation, there must be positive integers J and K such that 1 / 3 = J / ( K ). But 10 = 25, so this becomes 1 / 3 = J / ( K 5 K ). Cross multiplying, and recalling that everything is a positive integer, we have 3 J = ( K 5 K ) If the equation holds, there must be a “3” on the right hand side. But there cannot be a “3” on this side, as it is only 2’s and 5’s. Now, 0.20 = 1 / 5 has a terminating binary representation only if it has a representation of the form J / ( K ). This becomes 1 / 5 = J / ( K ), or 5J = 2 K
. But no 5’s on the RHS. Because numbers such as 1.60 have no exact binary representation, bankers and others who rely on exact arithmetic prefer BCD arithmetic, in which exact representations are possible.