Overview
Bits are the most basic unit of information in computing. Bit is short
for binary digit. They represents a logical state, which is one of two
possible values. It is either 1
or 0
, but can also be represented as
true
or false
, yes
or no
, on
or off
.
A Byte is meassuring size of Bits. Most of the time one byte represents 8 Bits.
Content
Data Type Sizes
In computing and programming, specific data types have a fixed size, representing the memory they occupy in the computer. To write efficient software, you should choose the right data type to avoid unnecessary memory usage.
For example:
If you need a variable for the age of a person, you just need a 8 bit
data type. Because:
which is sufficient.
You also have to take into account that if you need a negative number
to be represented, you need to use a signed
data type, which cuts the
available range in half. Instead of representing values from 0 to
255, a signed 8-bit integer can store values from -128 to 127.
How to represent a (positiv) integer number in binary
Imagine you want to represent the number 123
as a byte, meaning as
8 Bits. To do this, you can create a table with 8 columns, one for
each bit, along with his corresponding exponent. Below each column,
write the value of the bit’s exponent.
Then starting from left to right, check whether the exponent value fits into
the number you want to represent.
- If it does not, the bit value is
0
- If it fits, set the bit to
1
and subtract the exponent value from the remaining number - Repeat this process for the next exponent, always checking whether it fits into the current remainder
Here’s how the number 123
is represented in binary:
Exponents | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
---|---|---|---|---|---|---|---|---|
Exponent Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Bit | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
Calculation | 123 | 123 | 123-64=59 | 59-32=27 | 27-16=11 | 11-8=3 | 3 | 3-2=1 |
How to represent a signed integer dtype in binary
If you want to represent a signed dtype the leftmost bit, which is the Most Significant Bit (short: MSB), is reserved for the sign
- 0 → positiv number
- 1 → negativ number
Since the first bit is reserved for representing the sign, you lose a whole bit for number representation. This meands that signed data types have a smaller numerical range compared to unsigned data types.
If you want to represent a negative number, it is common that the twos complement is used. For this, the number is represented as described for unsigned numbers. But after this every bit is negated (1 to 0 and 0 to 1) and is added with a 1 as binary.
First represent 123 as above: negate it’s values:
Then add a 1
as binary
Representing floating point numbers in binary
Most floating-point numbers cannot be represented exactly in a
computer. Exact representation would only be possible if we had an
infinite number of bits. However, for the float
datay type we are
limited to 32 bits. Because of this, there is usually a rounding error
during representation, and the floating-point number is approximated
rather than stored exactly.
These 32 bits are divided into three parts to represent a floating-point number:
- 1 bit for the sign
- 8 bit for the exponent
- the exponent determines the position of the decimal point
- 23 bit for the mantissa
- the mantissa holds the significant digits of the number
- these bits correspond to negative exponents in base 2
a shortend example:
Exponent | sign bit | 2^8 | … | 2^2 | 2^1 | 2^0 | point | 2^-1 | 2^-2 | 2^-3 | 2^-4 | … | 2^-23 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Exponent Values | 256 | … | 4 | 2 | 1 | point | 0.5 | 0.25 | 0.125 | 0.0625 | … | 0.0000… |
To represent a floating-point number in binary, follow the same procedure as for integer numbers, but consider the exponent and mantissa structure.
Binary Counter
With increasing decimal numbers, the binary representation follows these rules:
- check the last binary digit: if it is
0
, change it to1
- if the last binary digit is
1
, change it to0
and check the next digit to the left - repeat this process untill you find a
0
, which you then change to1
The initial binary state is 0
, so the decimal number 0
is represented as
binary 0
. Since the smallest common datatype is 8 bits, it is actually
represented as 00000000
.
Example
Decimal | Binary |
---|---|
0 | 0 |
1 | 1 |
2 | 10 |
3 | 11 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
16 | 10000 |
17 | 10001 |
18 | 10010 |
19 | 10011 |
20 | 10100 |
21 | 10101 |
22 | 10110 |
23 | 10111 |
24 | 11000 |
25 | 11001 |
26 | 11010 |
27 | 11011 |
28 | 11100 |
29 | 11101 |
30 | 11110 |