- Richard Feynman -

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

(→05: Fixed bug) |
(→Links: Further reading) |
||

Line 306: | Line 306: | ||

=Links= | =Links= | ||

+ | ==Further reading== | ||

+ | * [https://www.exploringbinary.com/twos-complement-converter/ testa dig själv och labba med "Two's complement converter"] | ||

+ | * [http://www.free-test-online.com/binary/binary2decimal.htm Binary to Decimal Conversion of Unsigned Numbers] | ||

+ | * [http://www.free-test-online.com/binary/integers_signed.htm Binary to Decimal - Two's Complement Integers] | ||

==Navigation== | ==Navigation== | ||

Next page is [[ITIC:Software and programming introduction|Software and programming introduction]]. | Next page is [[ITIC:Software and programming introduction|Software and programming introduction]]. | ||

{{Nav links|prev=ITIC:Digital_representation_-_Binary|TOC=Introduction_to_IT_and_computing|next=ITIC:Software and programming introduction}} | {{Nav links|prev=ITIC:Digital_representation_-_Binary|TOC=Introduction_to_IT_and_computing|next=ITIC:Software and programming introduction}} |

## Revision as of 08:43, 12 September 2019

## Contents

# 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.

2^{9} = 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 2^{0} (1_{10}) if it is set (is a one). The next (going left) bit is worth 2^{1} (2_{10}) if it is set. The next (still going left) is worth 2^{2} (4_{10}). The next is worth 2^{3} (8_{10}) and the next is worth 2^{4} (16_{10}) etc.

- 1001
- 101
- 11
- 10
- 1111

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

Answers:

- 1*1 + 0*2 + 0*4 + 1*8 = 9
_{10} - 1*1 + 0*2 + 1*4 = 5
- 1*1 + 1*2 = 3
- 0*1 + 1*2 = 2
- 1*1 + 1*2 + 1*4 + 1*8 = 15

## 03

Write the following binary numbers in decimal and hexadecimal:

- 1001
_{2} - 101
_{2} - 11
_{2} - 10
_{2} - 1111
_{2}

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

Answers:

- 1001
_{2}: 9_{10}, 9_{16} - 101
_{2}: 5_{10}, 5_{16} - 11
_{2}: 3_{10}, 3_{16} - 10
_{2}: 2_{10}, 2_{16} - 1111
_{2}: 15_{10}, F_{16}

## 04

Write the following hexadecimal values in binary and decimal:

- B1
- A
- F
- 12
- 1A
- 20

**Hint: ** Going from the rightmost and moving left, the first bit is worth 16^{0} (1_{10}), the next is worth 16^{1} (16_{10}), the next is worth 16^{2} (256_{10}) etc. Also, remember that A_{16} is 10, B_{16} is 11 ... and F_{16} is 15. So a single C_{16} is (12*1)_{10}, and C1_{16} is (12*16 + 1 = 193)_{10}.

**Hint: ** You can translate each digit of the hexadecimal number into four bits. B_{16} is 11_{10}. 11_{10} is 1011_{2}. BB_{16} is 1011 1011_{2}. 3B_{16} is 0011 1011_{2} (first 3, then B).

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

Answers (calculation shows hex to decimal):

- B1: (1011 0001)
_{2}, (1 * 1 + 11 * 16 = 177)_{10} - A: (1010)
_{2}, (10*1)_{10} - F: (1111)
_{2}, (15*1)_{10} - 12: (0001 0010)
_{2}, (2*1 + 1*16 = 18)_{10} - 1A: (0001 1010)
_{2}, (10*1 + 1*16 = 26)_{10} - 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
- -2
- -3
- -4
- -65
- -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
_{10}is (1111 1111)_{2} - -2
_{10}is (1111 1110)_{2} - -3
_{10}is (1111 1101)_{2} - -4
_{10}is (1111 1100)_{2} - -65
_{10}is (1011 1111)_{2} - -127
_{10}is (1000 0001)_{2}

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

`1100 0001`

(1*2^{0} + 1*2^{6})_{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: -127_{10} 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:

- 40
_{8} - 50
_{8} - 101
_{8} - 172
_{8}

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

Evaluate the following octal numbers to decimal and hexadecimal:

- 40
_{8}- 32_{10}- 20_{16} - 50
_{8}- 40_{10}- 28_{16} - 101
_{8}- 65_{10}- 41_{16} - 172
_{8}- 122_{10}- 7A_{16}

Explanations (for a few of the above answers):

40_{8} means, in decimal, (working from right to left) 0*8^{0} + 4*8^{1} = 32_{10}.

We can recognize that (16 * 2 = 32)_{10}, so we get 20_{16} which is: 0*16^{0} + 2*16^{1}.

Answer: 40_{8} is 32_{10} and 20_{16}.

172_{8} means 2*8^{0} + 7*8^{1} + 1*8^{2} = 2 + 56 + 64. That is, 122_{10}.

7 * 16 + 10 = 122. So in hex, we get 7A. Remember, 7A means 10*16^{0} + 7*16^{1}.

Answer: 172_{8} is 122_{10} and 7A_{16}.

## 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:

33_{10} = 100001_{2}

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 33_{10}.

Answer: 33_{10} is 41_{8}.

Another example:

127_{10} = 1111111_{2}

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

Answer: 127_{10} is 177_{8}.

Translate the following decimal numbers to octal:

- 16
- 32
- 64
- 65

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

Translate the following decimal numbers to octal:

- 16
- 32
- 64
- 65

Answers:

16 is in binary 10000. Grouped in threes: 010 000 which then becomes 20_{8}.

32 becomes 100 000, which becomes 40_{8}.

64 becomes 001 000 000, which becomes 100_{8}.

65 becomes 001 000 001, which becomes 101_{8}.

# Links

## Further reading

- testa dig själv och labba med "Two's complement converter"
- Binary to Decimal Conversion of Unsigned Numbers
- Binary to Decimal - Two's Complement Integers

Next page is Software and programming introduction.

« Previous • Book TOC • Next »