Number representation – Bits

advertisement
Number representation – Bits
Numbers are stored in computers in base 2 (binary) as a series
of bits which can have the value 0 or 1. Every primitive data type
uses a specific number of bits. In Java, for example, an int is
stored in 32 bits.
The most significant bit (the leftmost bit) in numerical values
indicates the sign: 0 positive, 1 negative.
Real Numbers
Real numbers, float and double, are represented with as an
exponent followed by the significand (mantissa):
12.34 == 0 10000010 10001010111000010100010
exponent significand
Real math has a loss of precision:
12.34 * 1.7 = 20.978
System.out.println(12.34 * 1.7) ==> 20.977999999999998
1.5 decimal = 1.1 binary = 1x20 + 1x2-1
12.34 decimal = 1x23 + 1x22 + 0x21 + 0x20
0x2-1 + 1x2-2 + 0x2-3 + 1x2-4
0x2-5 + 1x2-6 + 1x2-7 + 1x2-8
0x2-9 + 0x2-10 + 0x2-11 + 0x2-12
1x2-13 + 0x2-14 + 1x2-15 + 0x2-16
0x2-17 + 0x2-18 + 1x2-19 + 0x2-20
1.75476074219e-6 (decimal)
+
+
+
+
+
+
Two's Complement – Negatives
Negative numbers are represented in the two's complement of
the positive number: invert each bit and add 1. Using 16 bits:
13
-13
==
==
0000000000001101
1111111111110011
---------------0000000000000000
Download