Computer arithmetic is different from the arithmetic that people use:

- Computers perform operations on numbers whose precision is finite and fixed.
- Computers represent numbers using the binary rather than the decimal system.

A decimal number is constructed by summing multiples of the digits:

i.e. thousands, hundreds, tens, units - for a four digit decimal number.

e.g. 576=5*100+7*10+6*1

The number to multiply each digit by is 10^{d} where d is the
position of the digit (0 for units, 1 for tens etc..). The base for this
power is called the **Radix.** When dealing with computers it is common
to use radices other than 10. The most important radices are 2 and 16.

Information is most commonly stored in a digital computer as the presence or absence of a current, a charge or a magnetic field. It is uncommon to find a storage device that can store more than two levels. For this reason, it is very common to encounter radix 2 (or binary) numbers.

In binary, each digit can only be 0 or 1. A binary digit is called a bit. The columns in a binary number represent powers of 2.

i.e. sixteens, eights, fours, twos, units - for a five digit binary number.

Decimal Binary

0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 16 10000 17 10001 18 10010 19 10011 20 10100To convert between binary and decimal representations, use the following methods:

decimal to binary:

- Find the largest power of two that is less than or equal to the number.
- if the no is greater than or equal to this power of two
- otherwise
- look at the next smallest power of two
- repeat from step 2 until there are no more powers of two left.

- print a 1.

Subtract this power of two from the number

- print a 0.

Powers of two

0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024Example: Print 481 in binary

- Largest power below 481 is 256

- 481 >= 256 so print 1

481-256=225

225 >= 128 so print 1

225-128=97

97>=64 so print 1

97-64=33

33>=32 so print 1

33-32=1

1<16 so print 0

1<8 so print 0

1<4 so print 0

1<2 so print 0

1>=1 so print 1

To check: 1+32+64+128+256=481

10011=1+2+16=19

Here is some C code to print a number in binary.

These notes contain example programs in C like the one above. If you are reading the notes online, then you can run the program by clicking on the "Run" button above. If you want to change the program, just modify the source code and Run it again.0123456789abcdef

Decimal Binary Hex

0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 8 1000 8 9 1001 9 10 1010 a 11 1011 b 12 1100 c 13 1101 d 14 1110 e 15 1111 fOne hex digit is four binary bits and so converting between hex and binary is simple.

010010101101

0100:1010:1101

4:a:d

This number is 4ad in hex.

38a=011:1000:1010=01110001010

Two hex digits are an 8 bit binary number 00,01,02..ff and can represent a decimal number from 0..255

Four hex digits are a 16 bit binary number and can represent a decimal number from 0..65535

In C, hexadecimal numbers have the prefix 0x and can be used in the same way as a decimal number.

e.g.

i=0x01f3e;

a=a+0x0100;The 'printf' function allows you to print hexadecimal numbers directly using %x in the format string.

To think about finite precision numbers consider the set of positive integers that can be represented using three decimal digits. There are 1000 such numbers from 0..999, but this representation can not store:

- Numbers larger than 999
- Negative numbers
- Fractions

- 800+300=1200 - (too large)
- 300-700=-400 - (negative)
- 300/600=0.5 (not an integer)