



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
An introduction to binary numbers and explains how to convert integers and fractions from decimal to binary in c++. It includes examples and quick reference tables.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
Programming in C++
Base 10 digits: 0 1 2 3 4 5 6 7 8 9 Base 2 digits: 0 1 Recall that in base 10, the digits of a number are just coefficients of powers of the base (10): 417 = 4 * 10 2 + 1 * 10 1 + 7 * 10 0 Similarly, in base 2, the digits of a number are just coefficients of powers of the base (2): 1011 = 1 * 2 3 + 0 * 2 2 + 1 * 2 1 + 1 * 2 0 Any real number can be represented in any base; humans generally use base 10, butcomputer hardware generally uses base 2 representation.
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
The base 10 system is also known as the decimal system; base 2 is referred to as binary. How can we convert an integer from decimal to binary? Here is a simple algorithm: While N > 0 Do WriteN <-- N %N (^2) / 2 //// remainderdivide N bywhen 2 N is divided by 2 Endwhile Note that the remainder will always be either 0 or 1, a binary digit or bit. The resultingsequence of bits is the binary representation of the integer N. See the next slide for an example...
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
Programming in C++
Find the binary representation of the decimal integer N = 23:
So the decimal integer N = 23 is represented in base 2 as: 10111 Let’s check that: 10111 = 12^4 + 12^2 + 12^1 + 12^0 == 1623 + 4 + 2 + 1 Note: we justshifted frombase 2 to base 10
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
Here are some handy facts:
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
Programming in C++
A general decimal number, like 43.375, would be converted to binary by converting the integer and fractional parts separately (as shown earlier) and combining the results. The decimal integer 43 would be represented in binary as:
The fractional part .375 would be represented in binary as: .
So 43.375 would be represented in binary as: 101011.
Integer Remainder
Carry Fraction
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
So 0.1 would be represented in binary as: 0.0 0011 0011 0011 0011….
Carry Fraction
Clearly, this pattern will nowrepeat forever
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA
Programming in C++
So 0.1 would be represented exactly in binary as: 0.0 0011 0011 0011 0011…. What would actually be stored in hardware? Suppose that we have a float x = 0.1; float variable: A float is stored in scientific form (but in binary): 0.0 0011 0011 0011 0011…. =(1.1 0011 0011 0011….) * 2 -
The exponent is stored using 7 bits and the fractional part is stored using 23 bits (withtwo bits used for the signs). We cheat and don’t store the first ‘1’, so 0.1 would be stored as: -0000100 +.1 0011 0011 0011 0011 0011 00
That’s -4in binary
Exponent Mantissa (fractional part)
Computer Science Dept Va Tech August, 1999 ©1995-1999 Barnette ND, McQuain WD, Keenan MA