Converting Number Bases

Decimal · Binary · Octal · Hex

It's important as Computer Science students to have a good understanding of the binary, hexadecimal and octal numbering systems.

  • As complex as they may seem, on a conceptual level computers are nothing more than boxes full of millions of "light switches."

    • Each of the switches in a computer is called a bit, short for binary digit.
    • A computer can turn each bit either on or off. Your computer likes to describe on as 1 and off as 0.
  • The binary number system uses only two values (0 and 1; off and on) to represent codes and data.

  • Since zeros and ones can be easily represented by two voltages, the binary system is the foundation on which digital technology is built.

    • By arranging bits in groups, the computer is able to describe more complex ideas than just on or off.
    • The most common arrangement of bits in a group is called a byte, which is a group of eight bits.

    • Every digital computer whether a pocket calculator or a mainframe uses the same binary notation

The following labs go through some practice with converting between all of the number bases and Binary Arithmetic in the final lab

Binary Conversions

  • Binary is a base 2 number system that was modified by Gottfried Leibniz to create today binary system

    • A binary code represents text, computer processor instructions, or any other data using a two-symbol system
  • Binary numbers are important because using them instead of the decimal system simplifies the design of computers and related technologies.

  • Binary is clearly seen with IP addressing (where an IP is a numeric identifier that represents a computer or device on a network)

    • The importance of understanding binary as a computer science student is necessary as you need to know the technical details behind IP addresses in order to recognise wider possibilities in managing a network.

EXERCISE: Using the notes provided or using an alternative method of your choice:

Where d=decimal number base and b=binary number base

  1. Convert 1584(d) to binary
  2. Convert 543(d) to binary
  3. Convert 10010011(b) to decimal
  4. Convert 101110(b) to decimal

Hexadecimal Conversions

  • As you can see most computer hardware and software use a binary representation.

    • Longer binary numbers are what computers use at the hardware level.
    • However, they can be very difficult to parse by a human being
  • We humans use power of 2 representations like octal and hexadecimal to read the binary representation in a more efficient and painless way.

  • Hexadecimal is used to denote colours in the “hex” colour scheme

    • a scheme that most computer users are somewhat familiar with.
  • QUESTION If a number has k digits in hex, how many digits (bits) does it have in binary?

    • ANSWER k/4 ... But think about why??

EXERCISE: Using the notes provided or using an alternative method of your choice:

Where d=decimal number base, b=binary number base and h=hexadecimal number base

  1. Convert 1764(d) to hex
  2. Convert 693(d) to hex
  3. Convert 10010011(b) to hex
  4. Convert 10111110010(b) to hex
  5. Convert 1B0(h) to binary (NOTE: a little more challenging. Recall the weighted positional notation/polynomial notation in converting binary to decimal and follow the same rules, replacing the 2^0 in the right position to 16^0 and so on. try it)
  6. If the colour yellow is a mix of the highest saturation of red and the highest saturation of green without any blue, what is the hex representation for the colour yellow?
  7. If Olive green is approximately half saturation of red and half saturation of green, what is the hex representation for olive green colour?
  8. What is the RGB colour code for the Hex representation of the colour blue?

A baker's dozen is D

Octal Conversions

  • Octal numbers are important because understanding their use is imperative to assigning file permissions in Unix.

    • Octal is used as a shorthand for representing file permissions on UNIX systems.
    • For example, file mode rwxr-xr-x would be 0755
  • Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right).

How are base 2 and base 8 are related?

Because 8 is a power of 2, base-8 digits can be read off in binary and 3 base-2 digits can be read off in octal.

EXERCISE: Using the notes provided or using an alternative method of your choice:

Where d=decimal number base, b=binary number base, h=hexadecimal number base and o=octal number base

  1. Convert 176(o) to hex
  2. Convert 693(h) to octal
  3. Convert 10010011(b) to octal
  4. Convert 136(o) to decimal
  5. You want to assign your file OctalLab.txt rwx for user, group and others. Using octal numbers, what is the Unix chmod command for setting these permissions?
  6. You want to assign your file OctalLab2.txt rwx for user, r-- for group and -w- for all others. Using octal numbers, what is the Unix chmod command for setting these permissions?

Binary Arithmetic in an IP Address

  • The act of creating large numbers from groups of binary units or bits is called binary arithmetic.

    • Learning binary arithmetic helps you understand how your computer sees IPs (or any numbers greater than one).
  • In binary arithmetic, each bit within a group represents a power of two.

    • Specifically:

      • the first bit in a group represents 2^0 (NB: n^0=1)
      • the second bit represents 2^1,
      • the third bit represents 2^2, and so on
    • each successive bit in a group is exactly twice the value of the previous bit.

In binary math, the values for the bits ascend from right to left, just as in the decimal system you're accustomed to:

Values for each Bit in a Byte

  • Now that we know how to calculate the value for each bit in a byte

    • creating large numbers in binary is simply a matter of turning on certain bits and then adding together the values of those bits.

    • So an 8-bit binary number 01101110 is therefore represented as:

Representation of 01101110

  • Here, the values 64, 32, 8, 4 and 2 are all turned on

    • So for the binary value in the table, 01101110, we add together 64+32+8+4+2 to get the number 110

How computers see IP addresses

  • an IP address is a 32-bit number subdivided into four bytes

    • For example 204.132.40.155
  • This is how your computer sees that IP:

    • 11001100.10000100.00101000.10011011

Why is each octet with an IP address in the range 0-255?

  • Calculate the highest number that a byte can represent:
    • If you turn on all the bits in a byte (11111111) and then convert that byte to a decimal number (128 + 64 + 32 + 16 + 8 + 4 + 2 + 1), those bits total 255.

Why is understanding this important to you?

  • Network administrators must know technically what's going on in order to implement anything but the simplest network
  • binary is a fundamental requirement for subnetting
  • being able to look at IP addresses the way your computer does will help you do a better job as a network administrator

Binary Arithmetic

  • In computers, the same operations are performed inside the central processing unit by the arithmetic and logic unit (ALU).

  • Arithmetic in binary is much like arithmetic in other numeral systems. Addition, subtraction, multiplication, and division can be performed on binary numerals.

    • base 2 arithmetic is easier than base 10 since you don't have to remember any tables for addition, subtraction, or multiplication

This lab will go through 1) Binary Addition 2) Binary Multiplication 3) Binary Subtraction including 2's Complement Method

1) Binary Addition

  • only have 2 digits to worry about, 0 and 1

  • line the two numbers up (one under the other), then, starting at the far right, add each column, recording the result and possible carry as we go

Here are the possibilities:

    0 + 0 = 0
    1 + 0 = 1
    1 + 1 = 2 which is 10 in binary which is 0 with a carry of 1
    1 + 1 + 1 (carry) = decimal 3 which is 11 in binary. In your  calculation, write down one 1 and a carry of 1

    These are the only possibilities in Binary Addition

WORKED THROUGH EXAMPLE: Perform binary addition on 11101 + 1100

Always align the numbers FROM THE RIGHT!

  • Start adding the numbers, starting from the RIGHT side..
Starting at the right side of the above calculation:

1+0=1 
0+0=0
1+1=1 and carry a 1 ... this is the first carry so this is what your arithmetic should look like, so far:

Add from the right, as far as first carry

  • Continue adding until you reach the last carry on the left Add the numbers, bringing any carry to the left as you go

    • It IS possible to add as many numbers as you wish, but the carry can get confusing.
      • Consider adding Number1+Number2 and then, with your answer add Number3 and so on

2) Binary Multiplication

  • Remember multiplication is basically repeated addition.
  • Again, we are only working with two numbers

Here are the only possibilities in binary multiplication:

    0 * 0 = 0
    1 * 0 = 0
    1 * 1 = 1

Easy peasy eh!

EXERCISES: Perform binary arithmetic on the following:

  1. add 1001101(b) to 1010000(b)
  2. add 100001101(b) to 1010001(b)
  3. multiply 1001101(b) to 1010000(b)
  4. if you wish, perform some more calculations based on whatever numbers you wish... converting decimal into binary and performing your arithmetic!
HINT: multiplication tip!!!!!!!


If we want to multiply a binary number by another number which is a power of 2 then all we need to do is add the number of 0's representing that power to the right of the first number.

eg. 8 is 2^3 which is 1000 in binary.

101101 x 1000 = 101101000

3) Binary Subtraction

  • Similar to binary addition, we will work through the numbers, column by column, starting on the far right.
  • Instead of carrying forward however, we will borrow backwards
Here are the possibilities:

    0 - 0 = 0
    1 - 0 = 1
    1 - 1 = 0

    0 - 1 we can't do so we borrow 1 from the next column. 
    This makes it 10 - 1 (decimal two minus one) which is 1.
    10 - 1 = 1
    11 - 1 = 1

WORKED THROUGH EXAMPLE 1: Perform binary subtraction on 11101 - 1100

Always align the numbers FROM THE RIGHT!

WORKED THROUGH EXAMPLE 2: How about 100 - 1

1    0    0
0    0    1    -    #leading zeros not necessary
_______________
  • Here we have 0-1 (zero minus one), and so we need to borrow on the top line
  • In the first column we need to borrow a 1 but the second column is 0.
  • Column 2 borrows a 1 from the third column and becomes 10 (or 2 in decimal)
0    10    0        #This is 10 is two in decimal
0    0    1    -    
_______________
  • Now we may borrow 1 from the second column (decreasing it from 2 to 1 or from 10 to 1)
0    1    10        #middle column decreased from 10 to 1
0    0    1    -
_______________
  • Now column 2 is 1 and column 1 is 10 and we may continue as normal.
0    1    10        #middle column Decreased from 10 to 1
0    0    1    -    #using the rules defined above 10-1=1 etc.
_______________
0    1    1        check your calculations using calculator if you wish

Sometimes you may have to do this over multiple columns but the process is the same.

How a computer subtracts: Two's Complement

The above example is the most convenient way for us to do binary subtraction by hand.

  • There is another approach however and this is the way that computers subtract binary digits.

  • This approach is called Two's Complement.

Binary Arithmetic

  • So far, we’ve been working with all positive numbers
  • Now we need a way to represent signed numbers like -18(base10) or -34(base10)

  • One solution is to add an extra digit to the front of our binary number to indicate whether the number is positive or negative.

  • In computer terminology, this digit is called a sign bit.

  • Remember that a "bit" is simply another name for a binary digit

  • When our number is positive, we make our sign bit zero, and

  • When our number is negative, we make our sign bit one. This approach is called the Signed Magnitude Representation

Examples

Since we are using 4-bit signed magnitude representation, we know the first bit is our sign and the remaining three bits are our number (Be sure that you do not mistake the number 1101(base2) for 13(base10)

The drawbacks of sign-magnitude representation are:

  1. There are two representations (0000 00002 and 1000 00002) for the number zero, which could lead to inefficiency and confusion.
  2. Positive and negative integers need to be processed separately.

Suppose we could represent signed numbers in such a way that all our computations could be completed using only addition. (This would allow our computer processor to perform all arithmetic operations with only addition circuitry.)

1's Complement

Representing a signed number with 1's complement is done by changing all the bits that are 1 to 0 and all the bits that are 0 to 1. Reversing the digits in this way is also called complementing a number

Again, the drawback is that there are two representations of zero, one being positive and one negative

Examples

Summary of 1's Complement

How a computer subtracts: Two's Complement

  • There is another approach however and this is the way that computers subtract binary digits.

  • This approach is called Two's Complement.

WORKED THROUGH EXAMPLE to compute 1000(b) - 11(b)**

Here, 1 is added to the 1's Complement representation

Step 1: Write the equation out, padding the bottom number with 0's

1000
0011 -

Step 2: Invert the digits of the lower number

1000
1100

Step 3: Add 1 b to the lower number (1100b + 1b = 1101b):

1000
1101

Step 4: Add those two numbers together (using the method above) to get 10101 b

Step 5: Remove the leading 1 (and any 0's after it). You are left with 101b.

Summary of 2's Complement