A primitive data type is a basic data type provided as a foundational building block
Complex data types are constructed on these
Casting is when you convert one data type to another
Eg: str(3) returns "3"
We can represent negative numbers in binary
This is done by using the most significant bit to represent sign
if the MSB is 0 then the number is positive
if the MSB is 1 then the number is negative
During two's complement the Most Significant Bit is a negative number
Eg: write -12 as a binary number using two's complement:
The MSB is -128 so this is our number
-12 fits into -128 so we have a 1 in our -128 column leaving 116
64 fits into 116
We can convert a positive binary number into its negative two's complement version:
Eg: 12:
12 in binary is 0 0 0 0 1 1 0 0
Now, to convert, starting from the right, copy each number until the first 1. After that, swap each 0 for a 1 and 1 for 0:
1 1 1 1 0 1 0 0
During binary addition:
0 + 0 = 0
0 + 1 = 1
1 + 1 = 0 carry 1
1 + 1 + 1 = 1 carry 1
All characters are either represented in ASCII or Unicode
Each character has its own unique bit pattern
A character set is the defined list of characters recognised by computer hardware and software with each character being represented by a unique bit pattenr
ASCII stands for American Standard Code for Information Interchange
ASCII is 7 bits
It can store 128 characters
Unicode was initially a 16 bit character set
However, now Unicode is a 24 bit character set, so it provides us over 16 million different characters, making it more universal as it includes characters from different languages as well as emojis
The main advantage of ASCII over Unicode is that it usually takes up much less space in memory than Unicode, despite representing less characters
Hexadecimal is in base 16
In hexadecimal:
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15
Computers do not use hexadecimal, but there is a close relationship between hexadecimal and a binary nibble, so they are useful for representing large binary numbers in a smaller number of digits, because they are just easier to read for humans.
NOTE: They take up the same space in a computer though
To convert denary to hexadecimal
convert denary number to binary
Then convert each nibble to hexadecimal
We can also store fractional numbers in binary
This is done by extending the number line to include negative powers of two. We place a binary point between 1 and 1/2
The process used to store fractions is called fixed-point binary
This however, limits the size of the number we can store, as bits are now being used to store the non-integer parts of the number
We can increase the accuracy of storing numbers by changing the position of the binary point, by making it float up and down, meaning that more bits are either used for the integer, or the real part of the number
We also need to store the position of the binary point
This is done by splitting the number into two parts
The mantissa : This is the number itself
The exponent : The position of the binary point in the number
We can shift binary numbers by moving all their bits to the left or to the right
If we shift it to the left, we multiply it
If we shift it to the right, we divide it
the number of times you shift it tells you the number you are multiplying or dividing by
When shifting, we knock off any bits that shift off from the left side
We pad any blank spaces with a 0
Eg: Shifting a binary number left 1 time multiplies it by 2, as 2^1 = 2
Shitfing a binary number right 2 times divided it by 4, as 2^2 = 4
A logical right shift will not work on negative numbers in two's complement will not work, as it turns the number positive
In an arithmetic right shift, the vacant MSB in the new number is filled with the value of the previous MSB
We can also apply masks to bit patterns
Masking allows us to :
isolate and extract bit values in a sequence of bits
Toggle, and set bit values in a sequence of bits
They use the operators AND OR XOR
Eg: use bitwise AND operation to apply a mask of 11110000 to the number 10011101
1 0 0 1 1 1 0 1
1 1 1 1 0 0 0 0
1 and 1 = 1
0 and 1 = 0
0 and 1 = 0
and so on
Masks are used in routing traffic on a network
Masks are used to isolate the networks and hosts in an IP address