Binary Representation
We've looked at how the number of bits used to represent an integer affects how big it can be. Now we turn to the specific way a singleword integer is encoded as bits.
:
Bit
DAT1.A.3
The word "bit" is an abbreviation for binary digit.
DAT1.C.1
People generally use base ten (decimal) digits to write numbers. Computers use base two (binary).
DAT1.C.3
In base 10, there are ten digits (09), and each place is worth ten times as much as the place to its right.
DAT1.C.2
In binary, base 2, there are only two digits (0 and 1), and each place is worth two times the place to its right.
The subscript 2 on 1101_{2} means the 1101 is in base 2. Numbers are normally written in base 10, so a subscript 10 is only used when needed for clarity.
Reading Binary
DAT1.C.5
In base 10 notation, each place value represents a power of ten: the units place (10^{0} = 1), the tens place (10^{1} = 10), the hundreds place (10^{2} = 100), the thousands place (10^{3} = 1000), etc. So, for example:
9827 = 9 × 10^{3} + 8 × 10^{2} + 2 × 10^{1} + 7 × 10^{0}
DAT1.C.4
Base 2 uses the same idea but with powers of two instead of powers of ten. Binary place values represent the units place (2^{0} = 1), the twos place (2^{1} = 2), the fours place (2^{2} = 4), the eights place (2^{3} = 8), the sixteens place (2^{4} = 16), etc. So, for example:
10010_{2} = 1 × 2^{4} + 0 × 2^{3} + 0 × 2^{2} + 1 × 2^{1} + 0 × 2^{0} = 16 + 2 = 18_{10}
To translate from binary (for example, 101101_{2}) to base 10, first, write the number out on paper. Then write out the binary place values by doubling left from the units place:
101101_{2} has only six digits, so we don't need powers of two to the left of that.
1 
0 
1 
1 
0 
1 
32 
16 
8 
4 
2 
1 

This means that this number is, in base 10, 32 + 8 + 4 + 1 = 45. So, 101101_{2} = 45_{10}.
DAT1.C part a

Translate these binary representations into base 10 notation:
 101_{2}
 111_{2}
 1010011_{2}
 1000000000_{2}
Writing Binary
To translate from base 10 (like 89_{10}) to base 2, first write out the binary place values by doubling left from the units place until you get to a value larger than your number (128 for this example). Then think, "I can take out a 64, so I write a 1 there, and there's 25 left (89 − 64). I have 0 thirtytwos, because I only have 25. But I can take out 16, and there's 9 left. So, 8 and 1 are the last nonzero bits.
Mary, this is a sloppy use of tables when I probably should be using some kind of CSS. Fix later. MF, 12/8/17 There is a commented out, incorrect sidenote here. bh

128 
64 
32 
16 
8 
4 
2 
1 

1 
0 
1 
1 
0 
0 
1 
I'm confused about why the arrow is going from right to left. Will that confuse students? Most of the work happens going left to right. Maybe there should be another arrow below the table that goes the other way... MF, 2/12/18 It may be left over from when we were using the correct algorithm, which does go right to left. bh
Now, read the number off: 1011001_{2} = 89_{10}.
Here's a more precise description of this algorithm to find the base 2 representation of any positive integer:
 First, find the largest power of two that is less than the number.
 Then, subtract that power of 2 from the number, keep the new number, and record a 1 in the place for that power of 2.

Then, determine if the next largest power of 2 that is less than the new number, and:
 If it does, subtract that power of 2 from the number, keep the new number, and record a 1 in the place for that power of 2.
 If it doesn't, keep the same number, and record a 0 for that power of 2.
Repeat this whole step with the next largest power of 2 until you have a bit (1 or 0) for all the remaining places down to and including the ones place (by which point you should have nothing left of the original number).
The string of ones and zeros you have recorded is the binary representation of your original number.
DAT1.C part a

Represent these base 10 numerals in binary (base 2):
 63
 64
 65
 129
 128
 127

DAT1.C part b
Put these numbers in ascending order without converting them to decimal notation:
 1011001_{2}
 11001_{2}
 100001_{2}
 101011_{2}
 What was your algorithm for comparing these numbers?

This question is similar to those you will see on the AP CSP exam.
A particular program uses 4 bits to represent whole numbers. When that program adds the numbers 9 and 7, the result is given as 0. Identify the best explanation of the result.
Data was corrupted during the operation due to a technical glitch.
While data corruption is possible, it is not a likely explanation for the recurrence of the result as one can demonstrate with repeated attempts to add 9 and 7.
The result was due to a roundoff error.
There is no rounding done here.
The result was due to an overflow error.
Correct. 9+7=16, which is beyond the capacity of 4 bits to express. 0=(0000)_{2} is the first and 15=(1111)_{2} is the last integer expressible in a 4bit system. 16=(10000)_{2} leads to an overflow.
A floatingpoint representation was used to approximate the result.
0 is not an approximation of 16.