Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -

# Difference between revisions of "ITIC:Digital representation - Binary - Exercises"

Jump to: navigation, search

# Exercises

## 01

a) How many combinations of 0s and 1s can you get from 9 bits?

b) How many positive numbers can you represent (including 0) and which numbers are that?

Expand using link to the right to see a suggested solution.

a) The number of combinations of 0s and 1s for nine bits, is equal 2 to the power of 9.

29 = 512.

Answer: 512 combinations.

b) If we start at 0, then the positive numbers we can represent are 0 through 511.

## 02

What decimal numbers are the following binary numbers?

Hint: The rightmost bit is worth 20 (110) if it is set (is a one). The next (going left) bit is worth 21 (210) if it is set. The next (still going left) is worth 22 (410). The next is worth 23 (810) and the next is worth 24 (1610) etc.

1. 1001
2. 101
3. 11
4. 10
5. 1111

Expand using link to the right to see a suggested solution.

Answers:

1. 1*1 + 0*2 + 0*4 + 1*8 = 910
2. 1*1 + 0*2 + 1*4 = 5
3. 1*1 + 1*2 = 3
4. 0*1 + 1*2 = 2
5. 1*1 + 1*2 + 1*4 + 1*8 = 15

## 03

Write the following binary numbers in decimal and hexadecimal:

1. 10012
2. 1012
3. 112
4. 102
5. 11112

Expand using link to the right to see a suggested solution.

Answers:

1. 10012: 910, 916
2. 1012: 510, 516
3. 112: 310, 316
4. 102: 210, 216
5. 11112: 1510, F16

## 04

Write the following hexadecimal values in binary and decimal:

1. B1
2. A
3. F
4. 12
5. 1A
6. 20

Hint: Going from the rightmost and moving left, the first bit is worth 160 (110), the next is worth 161 (1610), the next is worth 162 (25610) etc. Also, remember that A16 is 10, B16 is 11 ... and F16 is 15. So a single C16 is (12*1)10, and C116 is (12*16 + 1 = 193)10.

Hint: You can translate each digit of the hexadecimal number into four bits. B16 is 1110. 1110 is 10112. BB16 is 1011 10112. 3B16 is 0011 10112 (first 3, then B).

Expand using link to the right to see a suggested solution.

Answers (calculation shows hex to decimal):

1. B1: (1011 0001)2, (1 * 1 + 11 * 16 = 177)10
2. A: (1010)2, (10*1)10
3. F: (1111)2, (15*1)10
4. 12: (0001 0010)2, (2*1 + 1*16 = 18)10
5. 1A: (0001 1010)2, (10*1 + 1*16 = 26)10
6. 20: (0010 0000)2, (0*1 + 2*16 = 32)10

## 05

When converting a negative decimal number to binary using two's complement, you first make the leftmost bit into a one (signifying the minus sign), then you represent the number in binary, flip all bits and finally add one. In eight bits, the number -3 becomes:

```a) represent the number in binary with the leftmost sign bit set to one:
1000 0011
b) flip the bits of the seven value bits:
1111 1100
c) add one:

1111 1100
+0000 0001
----------
1111 1101

Answer: -3 is in two's complement, eight bits, 1111 1101
```

Write the following negative decimal numbers in eight bit binary using two's complement:

1. -1
2. -2
3. -3
4. -4
5. -65
6. -127

Expand using link to the right to see a suggested solution.

Suggested solutions:

Write the following negative decimal numbers in eight bit binary using two's complement:

1. -110 is (1111 1111)2
2. -210 is (1111 1110)2
3. -310 is (1111 1101)2
4. -410 is (1111 1100)2
5. -6510 is (1011 1111)2
6. -12710 is (1000 0001)2

To calculate -65, we represent it in binary but with the sign bit set to one:

`1100 0001` (1*20 + 1*26)10

Next, we flip the bits of the seven value bits:

```1011 1110
```

And then we add one:

``` 1011 1110
+0000 0001
----------
1011 1111
```

Answer: (-65)10 is (1011 1111)2 in eight bit binary, using two's complement.

To calcualte -127, we get the following steps:

```In decimal, 127 is 64 + 32 + 16 + 8 + 4 + 2 + 1, so the bits to set are:
0111 1111
|||| ||| `----2^0, which is 1
|||| |||
|||| || `-----2^1, which is 2
|||| ||
|||| | `------2^2, which is 4
|||| |
||||  `-------2^3, which is 8
||||
||| `---------2^4, which is 16
|||
|| `----------2^5, which is 32
||
| `-----------2^6, which is 64
|
`------------0x2^7, which is 0
```

Then we flip the bits:

```1000 0000
```

And then we add one:

``` 1000 0000
+0000 0001
----------
1000 0001
```

Answer: -12710 is in eight bit binary, using 2's complement: (1000 0001)2

## 06

Convert the following text to eight bit binary, using the ASCII table.

Hint: Newline is 10, space is 32

```Hello World!
In binary.
```

Expand using link to the right to see a suggested solution.

Suggested solution. Let's write the text, one character per line, with its's ascii value after it:

```character   ascii number

H           0100 1000
e           0110 0101
l           0110 1100
l           0110 1100
o           0110 1111
SPACE       0010 0000
W           0101 0111
o           0110 1111
r           0111 0010
l           0110 1100
d           0110 0100
!           0010 0001
NEWLINE     0000 1010
I           0100 1001
n           0110 1110
SPACE       0010 0000
b           0110 0010
i           0110 1001
n           0110 1110
a           0110 0001
r           0111 0010
y           0111 1001
.           0010 1110
```

Answer:

```Hello World!
In binary.
```

is in eight bit binary (using ASCII):

```0100 1000 0110 0101 0110 1100 0110 1100 0110 1111 0010 0000 0101 0111 0110 1111 0111 0010 0110 1100 0110 0100 0010 0001 0000 1010 0100 1001 0110 1110 0010 0000 0110 0010 0110 1001 0110 1110 0110 0001 0111 0010 0111 1001 0010 1110
```

## 06

Binary numbers use 2 as the base, decimal numbers 10 and hexadecimal numbers 16.

Octal numbers use 8.

Evaluate the following octal numbers to decimal and hexadecimal:

1. 408
2. 508
3. 1018
4. 1728

Expand using link to the right to see a suggested solution.

Evaluate the following octal numbers to decimal and hexadecimal:

1. 408 - 3210 - 2016
2. 508 - 4010 - 2816
3. 1018 - 6510 - 4116
4. 1728 - 12210 - 7A16

Explanations (for a few of the above answers):

408 means, in decimal, (working from right to left) 0*80 + 4*81 = 3210.

We can recognize that (16 * 2 = 32)10, so we get 2016 which is: 0*160 + 2*161.

Answer: 408 is 3210 and 2016.

1728 means 2*80 + 7*81 + 1*82 = 2 + 56 + 64. That is, 12210.

7 * 16 + 10 = 122. So in hex, we get 7A. Remember, 7A means 10*160 + 7*161.

Answer: 1728 is 12210 and 7A16.

## 07

Converting a decimal number to octal can be done by taking the binary representation of the number and dividing it into groups of three.

Example:

3310 = 1000012

Dividing it to groups of three (padding with zeros) 100 001 gives us the decimal numbers 4 and 1. That is also the octal representation of 3310.

Answer: 3310 is 418.

Another example:

12710 = 11111112

Dividing into groups of three gives us 001 111 111, so the octal number is 177.

Answer: 12710 is 1778.

Translate the following decimal numbers to octal:

1. 16
2. 32
3. 64
4. 65

Expand using link to the right to see a suggested solution.

Translate the following decimal numbers to octal:

1. 16
2. 32
3. 64
4. 65

Answers:

16 is in binary 10000. Grouped in threes: 010 000 which then becomes 208.

32 becomes 100 000, which becomes 408.

64 becomes 001 000 000, which becomes 1008.

65 becomes 001 000 001, which becomes 1018.

# Links

## Navigation

Next page is Software and programming introduction.