Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Do not use any tools or programming to solve these problems. Work it out yourself by hand, and fill in the answers.

Do not convert any binary numbers to decimal when solving a question unless the question explicitly tells you to.

The goal of these exercises is for you to gain an intuition for binary numbers. Using tools to solve the problems defeats the point.

Convert the decimal number 14 to binary. Answer: 14 = 8+4+2 = 1110

Convert the binary number 101101 to decimal: Answer: 32+0+8+4+0+1 = 45

Which is larger: 1000 or 0111? Answer: 1000 //1000 has a 1 in the 8s place; 0111 has nothing in the 8s place.

Which is larger: 00100 or 01011? Answer: 01011 //00100 has nothing in the 16s or 8s place; 01011 has a 1 in the 8s place.

What is 10101 + 01010? Answer: 10101 + 01010 = 11111

What is 10001 + 10001? Answer: 10001 + 10001 = 100010

What's the largest number you can store with 4 bits, if you want to be able to represent the number 0? Answer: 15 //4 bits gives 2⁴ = 16 distinct values. Representing 0 through 15.

How many bits would you need in order to store the numbers between 0 and 255 inclusive? Answer: 256 values = 2⁸, so 8 bits.

How many bits would you need in order to store the numbers between 0 and 3 inclusive? Answer: 4 values = 2², so 2 bits.

How many bits would you need in order to store the numbers between 0 and 1000 inclusive? Answer: 10 bits. //1001 values needed. 2⁹ = 512 (not enough), 2¹⁰ = 1024 (enough).

How can you test if a binary number is a power of two (e.g. 1, 2, 4, 8, 16, ...)? Answer: A power of two in binary has exactly one 1-bit and all the rest are 0s (e.g. 1, 10, 100, 1000…). So check that the number has a single 1 followed only by 0s. Equivalently, if you subtract 1, a power of two flips to all 1s in the lower bits — so n AND (n-1) equals zero for any power of two (and n itself is not zero).

Convert the decimal number 14 to hex. Answer: 0xE //14 = 16×0 + 14, and 14 in hex is E.

Convert the decimal number 386 to hex. Answer: 0x182 //386 ÷ 16 = 24 remainder 2 → least significant digit: 2 24 ÷ 16 = 1 remainder 8 → next digit: 8 1 ÷ 16 = 0 remainder 1 → most significant digit: 1

Convert the hex number 386 to decimal. Answer: 3×256 + 8×16 + 6 = 768 + 128 + 6 = 902

Convert the hex number B to decimal. Answer: 11 //B is the 11th hex digit (after 0–9, then A=10).

If reading the byte 0x21 as a number, what decimal number would it mean? Answer: 2×16 + 1 = 33

If reading the byte 0x21 as an ASCII character, what character would it mean? Answer: ASCII 33 is ! (exclamation mark)

If reading the byte 0x21 as a greyscale colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? Answer: 0x21 = 33 out of 255, which is very dark — a very dark grey, close to black.

If reading the bytes 0xAA00FF as an RGB colour, as described in "Approaches for Representing Colors and Images", what colour would it mean? Answer: R = 0xAA (170) — medium-high red; G = 0x00 (0) — no green; B = 0xFF (255) — full blue. This is a purple/violet colour (a fairly vivid bluish-purple).

If reading the bytes 0xAA00FF as a sequence of three one-byte decimal numbers, what decimal numbers would they be? Answer: 170, 0, 255 //0xAA = 10×16+10 = 170; 0x00 = 0; 0xFF = 15×16+15 = 255.