For this course, you need to know about 5 different data types:
- Boolean: Can be either true or false. - Character: Represents a single text symbol. (i.e: 'h') - String: Represents many characters. (i.e: "hello, world!") - Integer: A whole number. (i.e: 3) - Real: A number that can be expressed using decimal places. (i.e: 3.14)
In the construction of computers, we have developed our systems to use base 2 as on the lowest level, a computer is an electrical device that can turn signals either on or off.
Convert <math>10100011_2</math> to Denary.
1. <math>10100011_2</math>
2.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
4. 128 + 32 + 2 + 1
5. Result: 163
The hexadecimal counting system's main usein computer science is to shorthand represent a binary number.
Hexadecimal value tableHex | Binary | Denary |
---|---|---|
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
A | 1010 | 10 |
B | 1011 | 11 |
C | 1100 | 12 |
D | 1101 | 13 |
E | 1110 | 14 |
F | 1111 | 15 |
To convert binary to hex, simply look at the bits. Every 4 bits in a decimal value will represent a hexedecimal digit.
Convert <math>10100011_2</math> to hexadecimal.
1. <math>10100011_2</math> -> <math>1010_2</math> <math>0011_2</math>
2. <math>1010_2</math> = A16, <math>0011_2</math> = 316
2. Result: A316
Binary can be used to represent a negative value too. One of the ways we can do this is with Sign and magnitude.
In sign and magnitude, the number remains the same, except for the most significant bit. The MSB is turned into a 'sign' bit.
The sign bit is used to say if its positive or negative, but you sacrifice some of your range. (The range of a sign and magnitude value is +127 to -127)
Example: Convert <math>01100011_2</math> to a negative binary number using sign and magnitude: Output: <math>11100011_2</math> (Just change the most significant bit)
Converting a binary number to a negative version of itself is easy with two's complement. All you need to do is invert every bit in the sequence (so <math>00000111_2</math> (7) becomes <math>11111000_2</math>) and then add one (<math>11111000_2</math> becomes <math>11111001_2</math>).
2 binary numbers can be easily added with a strategy similar to 'Short hand addition'.
<syntaxhighlight> 100101 + 010101 ----------- 1001010 ANSWER 11 1 1 CARRY BITS (ADDED TO RESULT) </syntaxhighlight>
A binary number can be subtracted easily. However, it is easier to think of it as adding a negative number. When attempting to do this with sign and magnitude, you will realise however you cannot do this directly. This is one of the major downfalls of using sign and magnitude, and why more often Two's complement will be used, as it can be used to do this easily.
<syntaxhighlight> 11111001 (-7) + 00000011 ( 3) ----------- 11111100 (-4) ANSWER 1 CARRY BIT (ADDED TO RESULT) </syntaxhighlight>
A negative value in mantissa and exponent can be represented using sign and magnitude. The method is no different, you sacrifice the m.s.b (most significant bit) to represent the sign.
A negative value in mantissa and exponent can be constructed easily. The method is the same, one thing to consider is that decimal places are always positive
The hexadecimal counting system has 16 'digits'. We use hexadecimal as it can be used to easily represent binary shorthand, as the base of hex can be found using a power of 2 (2^4 = 16) This means one digit in hex, can be represented with a 4 bit value in binary. This makes converting between the two simple. Every 4 bits of a binary number can be matched to a digit in hex, and vice versa.
Hexadecimal value tableHex | Binary | Denary |
---|---|---|
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
A | 1010 | 10 |
B | 1011 | 11 |
C | 1100 | 12 |
D | 1101 | 13 |
E | 1110 | 14 |
F | 1111 | 15 |
You can find the denary equivalent by first converting each digit to binary, then converting the result to denary. Convert h'A3'
1. A= <math>1010_2</math> , 3= <math>0011_2</math>
2. <math>1010_2</math> <math>0011_2</math> = <math>10100011_2</math>
You can find the denary equivalent by multiplying the digits like this:
(Denary equivalent value of digit) x 16^(digit place) Convert h'A3'
1. A = 10, 3 = 3
2. 10 x 16^1 + 3 x 16^0
3. 10x16 + 3x1
4. 160 + 3
5. Result: 163
You can find the denary equivalent by first converting each digit to binary, then converting the result to denary. Convert h'A3'
1. A= <math>1010_2</math> , 3= <math>0011_2</math>
2. <math>1010_2</math> <math>0011_2</math> = <math>10100011_2</math>
3.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 0 | 0 | 1 | 1 |
4. 128 + 32 + 2 + 1
5. Result: 163