Binary number system
Specification
[edit | edit source]
Specification coverage
|
Introduction
[edit | edit source]In the previous chapter we looked at the common number systems and bases. We use different number bases, as humans tend to work with decimals and computers can only process data in binary. As Computer Science students, we need to know how binary works and how the computer carries out calculations in binary.
Unsigned binary
[edit | edit source]
There is a quick way of finding out the maximum values for a given number of bits. Using the formula below, you can find out what is the maximum decimal value you can have while using 8 bit binary.
Maximum decimal value = , referring to the number of bits.
Examples For example, for 8 bits: And of course 0 would be the minimum decimal value in unsigned binary. |
There is also a way to find out the maximum number of combinations, you simply use the following formula: , referring to the number of bits.
Examples For example, for 8 bits: Meaning that there are 256 possible combinations if you are using 8 bit binary. If you were using 2 bit binary, also known as a nibble, you would do the following: These would be the 4 combinations:
|
Unsigned binary arithmetic
[edit | edit source]Many binary arithmetic questions have multiple marks. In some cases you not only should, but must, show your working! |
Adding
[edit | edit source]To add two numbers together in binary, first line up the numbers in the same way as you would do column addition in decimal:
Examples 1 1 (carried digits)
0 0 1 1 0 0 1 0
+ 1 0 1 1 0 1 0 1
-------------------
= 1 1 1 0 0 1 1 1
Now add the columns starting from the right-hand side, remembering that you can only use 0s and 1s:
You can check your answer by converting all the numbers to decimal, carrying out the addition and then converting the answer back to binary. In this case the answer, the first number is 50, the second number is 181, so the answer should be 231. |
Multiplying unsigned binary integers
[edit | edit source]To multiply in binary, you multiply the first number by each of the digits of the second number in turn starting from the right-hand side (in the same way that you would do multiplication in decimal).This means you are either multiplying each digit by 0 or 1, which will give you either a 0 or 1 as the answer. You then do the same for the next digit, shifting your answers to the left as you would in decimal multiplication.
This can often be easier understood when you watch someone do it. YouTube has a multitude of videos on the subject. |
You then carry out a binary addition to find the final answer. For example, to multiply 11011 by 11:
Examples 1 1 0 1 1
x 1 1
-------------------
= 1 1 0 1 1
+ 1 1 0 1 1 0
-------------------
= 1 0 1 0 0 0 1
1 1 1 1 (carried digits)
Note the zero on the LSB (least significant bit) as the numbers have been shifted to the left. Again you can work this out by converting the binary to decimal to check your answer. In this case the first number is 27 (twenty-seven), the second number is 3 (three), so the answer is 81 (eighty-one). Rules of binary multiplication are the same as the ones for decimals:
|
Signed binary using two’s complement
[edit | edit source]
Two's complement is a method used to represent signed integers in binary form. This means that it can be used to represent positive and negative integers. This section shows how two's complement can represent negative integers.
Assume we want to convert the binary code 100111002 into decimal using two's complement:
Step 1 Write out the decimal (or denary, if you prefer) equivalent as show:
|
Step 1 |
cake day friday join us!!!
MSB | LSB | ||||||
---|---|---|---|---|---|---|---|
1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |