Bits and Bytes
Bits and Bytes
1 Introduction
In this activity you will learn about binary numbers. This activity was based on
material developed by Professor Saturnino Garcia of the University of San Diego. It is
used here with permission.
Before you begin, take a minute to assign roles to each member in your group. Try
to switch up the roles as much as possible: please don’t pick a role for yourself that
you have already done more than once. Below is a summary of the four roles; write
the name of the person taking that role next to the summary.
If your group only has three members, combine the roles of Facilitator and Process
Analyst.
For this and all future activities, the Facilitator should also take the role of the reader
and read the questions aloud to the group.
Facilitator Reads question aloud; keeps track of time and makes sure everyone
contributes appropriately.
Quality Control Records all questions and answers, and provides team reflection to
team and instructor.
Spokesperson Talks to the instructor and other teams. Compiles and runs programs
when applicable.
Process Analyst Considers how the team could work and learn more effectively.
Fill in the following table showing which group member is performing each role:
Role Person
Facilitator
Quality Control
Spokesperson
Process Analyst
1/18
BBI 1
Problem 3. For the numbers you just listed, give the binary representation of the number
that came before it. For example, if 4 (i.e. 100) was one of your answers, give the binary
representation of the number 3 (i.e. 11).
2/18
BBI 1
Problem 4. What is the smallest number that will require a certain number of bits?
Give your answer in decimal representation.
5 bits
6 bits
7 bits
Problem 5. Let X be the first number to require N bits. In terms of X, what will be the
first number to require N + 1?
Problem 6. Describe the pattern of the rightmost bit as you count up from zero to ten.
Problem 7. Describe the pattern of the second rightmost bit as you count up from zero
to ten. To help you see the pattern, add a 0 to the left of both zero and one, making
them 00 and 01.
Problem 8. How often does the third rightmost bit transition from 0 to 1 and from 1 to
0? Again, add 0’s to the left of the binary representation to get enough bits if needed.
Problem 9. Complete the following table to show the binary representation of the
numbers 11 to 15, using your knowledge of binary patterns you explored in the
previous questions.
Decimal Binary
11
12
13
14
15
Problem 10. The decimal number 64 is represented as 1000000 in binary. Fill in the
following table with the binary representation of the given numbers, again using the
patterns you have discovered up to this point.
3/18
BBI 1
Decimal Binary
64 1000000
65
66
67
68
Problem 11. (Advanced) Imagine that you start a binary counter at 0 and then add 1
to it over and over again. Fill in the following table to indicate how often each bit
position transitions from 0 to 1 or 1 to 0 (i.e. the frequency of transitions).
TIME REQUIRED:
Digit 8 9 5 9
Index 3 2 1 0
Weight 103 102 101 100
Problem 12. What weighted value is associated with the digit at index 2?
Problem 13. Does adding a 0 to the left of a decimal number (i.e. at the beginning)
change its value? For example, changing 759 to 0759. Explain.
4/18
BBI 1
Problem 14. Does adding a 0 to the right of a decimal number (i.e. at the end) change
its value? For example, changing 759 to 7590. Explain.
Problem 15. Complete the following table that will help convert a binary number to
its equivalent decimal value.
Bit 1 1 0 0 1
Index 4 3 2 1 0
Weight 20
Problem 16. What is the decimal representation of the binary number in the previous
question (i.e. 11001)?
Problem 17. Does adding a 0 to the left of a binary number (i.e. at the beginning)
change its value? For example, changing 101 to 0101. Explain.
Problem 18. Does adding a 0 to the right of a binary number (i.e. at the end) change
its value? For example, changing 101 to 1010. Explain.
• 111011 =
• 1000001 =
Problem 20. We can calculate the value of an m digit decimal number according to
the following equation: X
di × 10i
i=0..m
Where di is the digit at index i (e.g. d3 in the example above is 8). Write a similar
equation, except to handle binary instead of decimal numbers.
5/18
BBI 1
Problem 21. One side effect of the equation you wrote in the last question is that any
value can be represented as a sum of powers of 2. For example, 3 is 21 + 20 (i.e. 2 + 1)
What powers of 2 add together to equal the following numbers.
• 4=
• 6=
• 7=
• 20 =
• 48 =
Problem 22. Fill in the following table of bits for the decimal number 48.
Bit
Index 6 5 4 3 2 1 0
Weight 20
Binary Representation
Decimal Representation
TIME REQUIRED:
6/18
BBI 1
Problem 26. Convert the binary number 1110011 into its equivalent decimal represen-
tation.
Problem 27. Convert the decimal number 99 into its equivalent binary representation.
D H D H D H D H
0 0 5 5 10 A 15 F
1 1 6 6 11 B 16 10
2 2 7 7 12 C 17 11
3 3 8 8 13 D 18 12
4 4 9 9 14 E 19 13
Note that we’ll often use the C notation of adding a 0x prefix to a hexadecimal
number to differentiate it from decimal (e.g. 150 vs 0x150).
Problem 28. What possible values can a single hexadecimal digit have? For example,
a single bit can have the value 0 or 1, while a single decimal digit can have values
between 0 and 9.
Problem 29. How many distinct values can a single hexadecimal digit represent? For
example, a decimal digit can have 10 distinct values while a binary digit (i.e. bit) can
have 2 distinct values.
Problem 30. What is the weight of the rightmost hex digit? Recall that the rightmost
decimal digit had a weight of 100 .
7/18
BBI 1
Problem 31. What is the weight of the hex digit just to the left of the rightmost digit
(i.e. the second rightmost)?
Problem 32. Give the hexadecimal and decimal representations of the first number to
require 3 hex digits.
• Hexadecimal:
• Decimal:
Problem 33. Which representation will be more compact (i.e. require fewer digits) for
large numbers: hexadecimal or decimal?
Problem 34. What is the hexadecimal representation of the decimal number 21?
Problem 36. Complete the following table by converting the hex numbers into their
equivalent decimal representation:
Hex. Dec.
0x30
0x5A
0x11D
Problem 37. One benefit of hexadecimal representation is the ease with which we can
convert between it and binary representation. Complete the table below, showing the
conversion between a hex digit and binary.
8/18
BBI 1
Problem 38. There is a trivial algorithm to convert from hex to binary: just convert
each hex digit to its equivalent 4-bit binary representation. For example, 0xA1 is
1010 (A) followed by 0001 (1), i.e. 10100001. What is the binary representation of the
following hex numbers?
• 0xCAFE =
• 0x8BADF00D =
Problem 39. Devise an algorithm for converting any binary number into its equivalent
hexadecimal representation. Your algorithm should be a direct conversion: you should
NOT convert to any intermediate representation (e.g. decimal).
Problem 40. Convert the following binary numbers into their hexadecimal represen-
tation:
• 110111 =
TIME REQUIRED:
Problem 42. (Advanced) What if we tried to use powers of −2? Would it still be possible
to represent any whole number? Are there numbers that can be represented as a sum
of powers of −2 but that can’t be represented as a sum of powers of 2?
9/18
BBI 1
Problem 43. Perform the following decimal addition using the trusty gradeschool
algorithm of adding digit-by-digit, carrying over from one column to the next when
necessary. Make sure you write down all your work (including carries).
8 1 5 2 9
+ 1 2 2 5 6
Problem 44. What is the maximum number that can result from adding two 1 decimal
digit numbers together?
Problem 45. As a result of your answer to the previous question, decimal addition
occasional requires that we carry a value from one column to the next. What is the
maximum carry value?
Problem 46. Complete the following table to indicate the result of adding two 1 bit
numbers (b1 and b2 ). The result of addition should also be written in binary notation.
b0 b1 b0 + b1
0 0 0
0 1
1 0
1 1
Problem 47. What is the maximum number of bits required to represent the value of
adding two 1-bit numbers?
Problem 48. Like decimal addition, binary addition includes the possibility of having
to carry a 1 value over to the next column and therefore have to add three values
together instead of two. Complete the following table to show all possible combinations
of adding two 1-bit numbers (b0 and b1 ) with a carry bit (c).
10/18
BBI 1
c b0 b1 c + b0 + b1
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 10
1
1
1
1 1 1
Problem 49. What is the maximum number of bits required to represent the value of
adding two 1-bit numbers and a carry?
Problem 50. Apply what you have learned from the previous questions to add the
following two binary numbers together.
1 0 0 1
+ 0 1 0 1
Problem 51. Convert the three binary numbers from the previous question (i.e. the
two numbers being added together and the result) to their decimal representation.
Was the result what you expected?
1 0 1 0
+ 1 1 0 0
Problem 53. How many bits were required for the result in the previous question?
11/18
BBI 1
Problem 54. How does the number of bits needed for the result compare to the number
of bits in the numbers you added together?
Problem 55. Come up with an equation that describes the maximum number of bits
required to hold the result of adding two N-bit numbers together.
Problem 56. What happens if you only have N bits to hold the result of two N-bit
numbers?
TIME REQUIRED:
Problem 58. Convert the binary number 1111011 to its equivalent hex representation.
12/18
BBI 1
• Step 1: Start by finding the binary representation of the magnitude of the number
(like we’ve done before).
Problem 59. What is the leftmost bit in a non-negative two’s complement number?
Problem 60. What is the two’s complement representation of the following numbers?
• 32 =
• 42 =
• Step 1: Start by finding the binary representation of the absolute value of the
number (like we’ve done before).
• Step 2: Place a 0 to the left of the number. This gives you the positive represen-
tation of the number.
• Step 4: Add 1. If this addition has a “carry out” from the leftmost pair of bits,
discard it. This gives you the negative representation of the number.
You might have noticed that the first two steps are the same for both negative and
non-negative values, so it’s really just two more steps to go from non-negative to
negative.
The following table shows the decimal numbers 0 to 10, their magnitude representa-
tion, their positive binary representation, the positive number with bits inverted, and
the negative representation (i.e. inverted + 1). These columns correspond to steps 1 to
4 from above.
13/18
BBI 1
Problem 61. What is the two’s complement representation of the following decimal
numbers?
• 3=
• 8=
Problem 62. Based on the table above, is there an easy way to determine the sign of a
two’s complement number?
Problem 63. Follow the four steps above to find the representation(s) of -15 in two’s
complement.
• Step 1:
• Step 2:
• Step 3:
• Step 4:
Problem 64. Fill in the following table by performing steps 3 and 4 from above (i.e.
invert bits and add 1) but this time starting with the negative representation.
14/18
BBI 1
Problem 65. Compare the “Add 1” column of this table with the table of decimal
numbers from 0 to 10, above. Do you notice any correspondence between them?
Problem 66. What is the decimal representation of the following two’s complement
binary numbers:
• 010111 =
• 111010 =
Problem 67. Complete the following table by converting the two’s complement
representation to the equivalent decimal representation.
Problem 68. How would you make an N-bit two’s complement number into an
(N + 1)-bit two’s complement number without changing its value?
Problem 69. Some of the entries in the “Negative” column in the first table have more
bits than required to represent that number. Identify these entries and give the minimal
two’s complement representation (i.e. minimize the number of bits without changing
the value that it represents).
15/18
BBI 1
Problem 70. Complete the following table to indicate the most positive (i.e. largest)
and most negative (i.e. smallest) integer that can be represented with a given number
of bits when using two’s complement representation. Use the table above (which you
simplified by removing unnecessary bits) to help you answer this question.
Problem 71. Use your answer from the previous question to find an expression that
gives the most positive integer that can be represented by a N-bit two’s complement
number. Hint: This will be related to a power of two in some way.
Problem 72. Use your answer from the previous questions to find an expression that
gives the most negative integer that can be represented by a N-bit two’s complement
number. Hint: This will be related to a power of two in some way.
Problem 73. In general, given an N-bit two’s complement number, how does the
magnitude of the most positive integer it can represent compare with the magnitude of
the most negative integer it can represent? Why might this be?
Problem 74. How many distinct integers can be represented by an N-bit two’s com-
plement representation (i.e. what is the difference between the most positive and the
most negative)?
Problem 75. Perform binary addition on the two’s complement numbers below:
1001
+ 0011
16/18
BBI 1
Problem 76. What are the decimal representations of your inputs (1001 and 0011) and
of your solution from the previous question? Is this the answer you expected?
Problem 77. Devise an algorithm for performing binary subtraction but using only
addition.
TIME REQUIRED:
1111000
+ 0100111 +
Problem 80. Give the decimal representation of the following two’s complement
binary numbers:
• 11 1111 1111 =
• 01 1010 0010 =
Problem 81. Give the two’s complement representation of the following binary
numbers:
17/18
BBI 1
• 105 =
• −482 =
Problem 82. What is the range of numbers that can be represented by a 32-bit two’s
complement number?
18/18