1.4.1 - Data Types - Using binary Flashcards
Primitive data types
One provided by a programming language
Integer, float, char, string, Boolean
Denary
Base 10/decimal
Our number system
How to know what data type a value is in
It will have 2/10/16 written in subscript
Binary
Uses 1/0 multiplied by the base, starting from the right
The bases are all powers of 2
Converting from denary to binary
Put a 1 in the column that is closest to the denary value, subtract and repeat
Hexadecimal
Uses base 16
Letters A-F represent values 10-15
Hexadecimal advantages
Quicker
Easier to understand
Less likely to make errors
Converting hex to denary
Show workings
Convert letters to digits, multiply by base and sum
Converting denary to hex
Divide by 16, add the hex symbol of the remainder assuming 2 bit value
Hex and binary conversions
Each hex value is 4 bits of binary
Convert the hex or binary to digit and then write out as the other
Combine the outputs
Binary addition
Works similar to denary, carry over a 1 if you have to go back to 0
What happens if adding 2 8-bit numbers gives a 9-bit answer?
There is an overflow error and the computer just cuts off the last bit from its output
Clearly indicate this in your answers
Range of Binary Addition
0 -> (2^n) -1 where n is the number of bits
Binary Subtraction
You make the second number into its negative value using Two’s Complement and then add them
Sign and Magnitude
The first bit represents +/- and then the rest are the same
The first bit being a 1 means the number is negative.
Two’s Complement Grid
The leftmost column has the negative value of itself
Sign and Magnitude Range
-(2^(n-1) + 1 -> (2^)n-1)) -1
Two’s Complement Process
- Find the positive value
- Invert the value of each bit
- Add one
Two’s complement range
- (2^(n-1)) -> 2^(n-1) - 1
What happens if there is overflow in binary subtraction?
You can ignore those bits
What are the lsb and msb
lsb - least significant bit - the rightmost bit
msb - most significant bit - the leftmost bit
Logical shift left
Pushes every value 1 space to the left, the msb goes into the carry bit, 0 comes into the lsb
Logical shift right
Pushes every value 1 space to the right, the lsb goes into the carry bit, 0 comes into the msb
Logical shift sums
x2 for left, /2 for right as long as it isn’t two’s complement or sign and magnitude
Arithmetic shift left
Pushes every value 1 space to the left but the msb doesn’t move so the second bit from the left goes into the carry bit, 0 comes in as lsb
Arithmetic shift right
Pushes every value 1 space to the right and the value that comes into the msb is whatever was there before
Arithmetic shift sums
x2 for left, /2 for right, for all numbers
Circular shift left
Pushes every value 1 space to the left, the msb into the carry bit and the carry bit into the lsb
Circular shift right
Pushes every value 1 space to the right, the lsb into the carry bit and the carry bit into the msb
Bitwise Masks
Apply another binary value with an AND, OR or XOR
Use boolean algebra on binary values by using each pair of corresponding values
OR masking
0: The output is the same as the input
1: The output is 1 (set the bit)
AND masking
0: The output is 0 (clear the bit)
1: The output is the same as the input
XOR masking
0: The output is the same as the input
1: The output is the opposite of the input (toggle the bit)
Fixed point binary
At a fixed point is a metaphorical decimal point, after that comes negative values of 2
First 5 coefficients after the fixed point
0.5, 0.25, 0.125, 0.0625, 0.03125
How does moving the decimal point affect it?
Having more bits before the decimal point increases the range but decreases the accuracy
What is a floating point binary number made up of
A mantissa and an exponent
mantissa x 2^exponent
Where is the decimal point at the start in floating point binary?
Between the first 2 bits
Positive floating point -> decimal
- Find the value of the exponent
- Move the decimal point by that many spaces to the right
- Find the value of that as if it was a fixed point decimal
Negative floating point -> decimal
- Find the two’s complement of the mantissa
- Find the value of the exponent
- Move the decimal point that many spaces to the right
- Find the value as if it was a fixed point decimal
- Put a sign in front of your value
How to know if a mantissa/exponent is negative
It will have a 1 as the msb (in two’s complement)
Negative Exponents decimal conversions
- Find the value of the exponent remembering the negative left value
- Move the decimal point that many places to the left, as if there were all 0s to the left of your number
- Find the value as if it was a regular fixed point number
Doing two’s complement on a binary number with a point
Add the 1 to the lsb
Normalised positive number
Has a sign bit 0 and a 1 for the next bit
Normalising a positive number
- Move the decimal point to the right until it is in front of the 1
- Add as many zeroes to the end as you moved right by
- Subtract the amount of spaces you moved to the right from the exponent
- Write out the normalised number with the mantissa and exponent
Normalised negative number
Has a sign bit 1 and a 0 for the next bit. Leading 1s don’t change the value.
Normalising a negative number
- Move the decimal point to the right until it is in front of a 0
- Add as many zeroes to the end as you moved right by
- Subtract the amount of spaces you moved to the right from the exponent
- Write out the normalised number with the mantissa and exponent
Positive denary -> floating point binary
- Convert it to fixed point binary
- Move the decimal point to the left until it is in front of the first 1
- Pad the RHS with zeroes to keep the same length if necessary
- Set the exponent equal to how many spaces you moved to the left
Negative denary-> floating point binary
- Convert the positive number to fixed point (write a 0 at the start)
- Take the two’s complement
- Convert as you would a positive number
Floating point addition
- Convert each floating point number to fixed point binary by shifting the binary point right by the value of the exponent
- Sum the two values
- Convert back to floating point and normalise
Floating point subtraction
- Convert each floating point number to fixed point binary by shifting the binary point right by the value of the exponent
- Take the two’s complement of the negative value
- Sum the two values
- Convert back to floating point and normalise