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."
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.
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 is a base 2 number system that was modified by Gottfried Leibniz to create today binary 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)
EXERCISE: Using the notes provided or using an alternative method of your choice:
Where d=decimal number base and b=binary number base
As you can see most computer hardware and software use a binary representation.
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
QUESTION If a number has k digits in hex, how many digits (bits) does it have in binary?
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
A baker's dozen is D
Octal numbers are important because understanding their use is imperative to assigning file permissions in Unix.
Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right).
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
The act of creating large numbers from groups of binary units or bits is called binary arithmetic.
In binary arithmetic, each bit within a group represents a power of two.
Specifically:
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:
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:
Here, the values 64, 32, 8, 4 and 2 are all turned on
an IP address is a 32-bit number subdivided into four bytes
This is how your computer sees that IP:
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.
This lab will go through 1) Binary Addition 2) Binary Multiplication 3) Binary Subtraction including 2's Complement Method
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
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:
Continue adding until you reach the last carry on the left
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:
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
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
WORKED THROUGH EXAMPLE 2: How about 100 - 1
1 0 0
0 0 1 - #leading zeros not necessary
_______________
0 10 0 #This is 10 is two in decimal
0 0 1 -
_______________
0 1 10 #middle column decreased from 10 to 1
0 0 1 -
_______________
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.
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.
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
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:
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.)
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
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.