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