As digital circuits (i.e. computers) only deal with binary data (1s and 0s) - there exists different encoding schemas to represent different data types.
ASCII is a representation of the common characters used throughout the world (i.e the keys on your keyboard)
Positive integers are represented as their binary representation - bound to the data type assigned
i.e. the byte 81 == 0b01010001
Negative integers are represented as the two’s complement of their binary representation.
- Two’s Complement -
Invert the bits, and add1
.
(byte) 81 == 0b01010001
(byte) -81 == ~0b01010001 + 1
== 0b10101110
+ 1
== 0b10101111 <--- Two's complement of 81
Given the representation of the byte 81
81 == 0b01010001
-81 == 0b10101111
We can verify two’s complement works by adding both binary numbers together (ie 81 + -81 = 0
)
(byte) 0b01010001 + 0b10101111 = 0
Floating Point numbers (i.e. 103.121369126) are represented by the IEEE 754
standard.
The decimal part of a floating point number is represented as the sum of negative powers of 2
i.e. 0.75 = $\frac{1}{2}$ + $\frac{1}{4}$
There exists a single and double precision variant.
Single Precision | Double Precision |
---|---|
First bit - sign Next 8 bits - exponent Next 23 bits - fraction | First bit - sign Next 11 bits - exponent Next 52 bits - fraction |
1) Convert the floating point number into binary, keeping the decimal
2) Turn the floating point number into its scientific form (one digit on the left of the decimal)
3) Convert the exponent into its binary representation and add it to a bias value ($ 2^n -1 $)
4) Answer is (sign)(exponent+bias)(fraction)
1) Note the sign of the value
2) Subtract the bias ($ 2^n -1 $) from the exponent
3) Assemble the floating point 1.(fraction)
4) Shift the decimal place by the value from (2)