## FoCS 02 - Positional notation

### Every N plus 1

The story of cover: the first integreted circuit made by human.

## Positional notation

In Positional notation, the value of content is determined by every character and its position.

## Positional Numeral Systems

Positional Numeral Systems is a method for counting made by human, which is a method of positional notation.
For any of Positional numeral system, for example, N base, which means the front number will add 1 and current number becomes 0 when the current number meet N

• For decimal, it will add 1 to the front number and current number becomes 0 when current number meet 10
• For Hexadecimal, it will add 1 to the front number and current number becomes 0 when current number meet 16
• For binary, it will add 1 to the front number and current number becomes 0 when current number meet 2
• And so on…

So in N based, the character set of every position is `{0, 1, 2, 3,..., N-1}`, we call the N base number.

For a N based number and its length is K, the maximum is `N^K - 1` .

### Decimal

Decimal is the most common positional numeral system in our daily life. The character set in decimal includes: 0-9.

For any decimal number, like `1938.5` and `-224.2` :

 ``````1 2 3 4 5 `````` ``````1938.5 = 1 * 10^3 + 9 * 10^2 + 3 * 10^1 + 8 * 10^0 + 5 * 10^(-1) = 1000 + 900 + 30 + 8 + 0.5 -224.2 = (-2) * 10^2 + (-2) * 10^1 + (-4) * 10^0 + (-2) * 10^(-1) = (-200) + (-20) + (-4) + (-0.2) ``````

For a decimal number and its length is K, the maximum is `10^K -1` .

### Binary

Binary is the most common positional numeral system in computer.

Why computer cannot use decimal like human?

Because most of electrical components only have two stable status, like: the transistor’s on and off, the voltage’s high and low, > and Magnetic or not. Binary design can simplify the physical design of computer and improve the stability of the computer. (In theory, we can also build a computer which is not build on binary system)

For any binary number, like `1011.1` and `-110.1` :

 ``````1 2 3 4 5 `````` ``````1011.1 = 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 2 * 2^0 + 1 * 2^(-1) = 1000 + 10 + 1 + 0.1 -110.1 = (-1) * 2^2 + (-1) * 2^1 + 0 * 2^0 + (-1) * 2^(-1) = (-100) + (-10) + (-0.1) ``````

For a binary number and its length is K, the maximum is `2^K -1` .

As you can see, it’s too long when using binary to present a big number, but decimal cannot store in computer directly like binary, and it’s not convinent to convert between binary and decimal.

Oct（short for Octonary）and Hex (short for Hexadecimal) is for solving problem I mentioned above. We can talk about Hex first, which is more commonly used.

For presenting a number is a Hex number, we need to use `0x` as prefix, like:

 ``````1 2 `````` ``````0x123456 0x6AF ``````

Its character set includes: `{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}`

In hex number system, here’s the mapping relationship between alphabet and number:

 ``````1 2 3 4 5 6 `````` ``````A ----> 10 B ----> 11 C ----> 12 D ----> 13 E ----> 14 F ----> 15 ``````

For any Hex number, like `0x2D.F` :

 ``````1 2 `````` ``````0x2D.F = 2 * 16^1 + D * 16^0 + F * 16^(-1) Note: Hex number system can be used to present a decimal, but it's not common. ``````

For a hex number and its length is K, the maximum is `16^K - 1` .

### Octonary

For presenting a number is a Oct number, we need to use `0` as prefix, like:

 ``````1 2 `````` ``````01234 02234 ``````

It’s character set includes: `{0, 1, 2, 3, 4, 5, 6, 7}`

For any oct number, like `023.7`:

 ``````1 2 `````` ``````023.7 = 2 * 8^1 + 3 * 8^0 + 7 * 8^(-1) Note: oct number system can be used to present a decimal, but it's not common. ``````

For a oct number and its length is K, the maximum is `8^K - 1` .

## Conversion

### Others to decimal

Convert every number to decimal and add them all.

#### Binary to decimal

Like `110.11` :

 ``````1 2 3 `````` ``````110.11 = 1 * 2^2 + 1 * 2^1 + 0 * 2^0 + 1 * 2^(-1) + 1 * 2^(-2) = 4 + 2 + 0 + 0.5 + 0.25 = 6.75 ``````

#### Hex to decimal

Like `0x2AF.CB` :

 ``````1 2 3 `````` ``````0x2AF.CB = 2 * 16^2 + A * 16^1 + F * 16^0 + C * 16^(-1) + B * 16^(-2) = 2 * 16^2 + 10 * 16 + 15 * 1 + 12 * 0.0628 + 11 * 0.00390625 = 687.79656875 ``````

As you can see, using hex to presenting a decimal is not convinent.

#### Oct to decimal

Like `0163.5` :

 ``````1 2 3 `````` ``````0163.5 = 1 * 8^2 + 6 * 8^1 + 3 * 8^0 + 5 * 8^(-1) = 64 + 48 + 3 + 0.625 = 115.625 ``````

As you can see, using oct to presenting a decimal is not convinent either.

### Decimal to others

The whole process of conversion has two parts: the integer part and the decimal part

• Integer part: using continuous division, deviding by base number until the quotient is 0 and putting all remainder in reverse order.
• Decimal part: using continuous multiplication, multiply by base number until the decimal is 0 and putting all integer in order.

#### Decimal to binary

Convert decimal number `17.625` to binary

Integer part:

 ``````1 2 3 4 5 6 7 8 `````` `````` Remainder Additional explanation 17 / 2 = 8 1 8 / 2 = 4 0 4 / 2 = 2 0 2 / 2 = 1 0 1 / 2 = 0 1 the quotient is 0, finish putting all remainder in reverse order (from bottom to top): 10001 ``````

Decimal part:

 ``````1 2 3 4 5 6 `````` `````` Integer Additional explanation 0.625 * 2 = 1.25 1 the decimal part of 1.25 is 0.25 0.25 * 2 = 0.5 0 the decimal part of 0.5 is 0.5 0.5 * 2 = 1 1 the decimal part of 1 is 0, finish putting all integer in order (from top to bottom): 101 ``````

The result: `10001.101`

#### Decimal to hex

Convert decimal number `17.625` to hex

Integer part:

 ``````1 2 3 4 5 `````` `````` Remainder Additional explanation 17 / 16 = 1 1 1 / 16 = 0 1 the quotient is 0, finish putting all remainder in reverse order (from bottom to top): 11 ``````

Decimal part:

 ``````1 2 3 4 `````` `````` Integer Additional explanation 0.625 * 16 = 10 A the decimal part of 10 is 0, finish putting all integer in order (from top to bottom): A ``````

The result: `0x11.A`

#### Decimal to oct

Convert decimal number `17.625` to oct

Integer part:

 ``````1 2 3 4 5 `````` `````` Remainder Additional explanation 17 / 8 = 2 1 2 / 16 = 0 2 the quotient is 0, finish putting all remainder in reverse order (from bottom to top): 21 ``````

Decimal part:

 ``````1 2 3 4 `````` `````` Integer Additional explanation 0.625 * 8 = 5 5 the decimal part of 10 is 0, finish putting all integer in order (from top to bottom): 5 ``````

The result: `021.5`

### Binary ⇄ Hex

#### Binary to hex

The process of Conversion has two parts as well: the integer part and the decimal part:

• Integer part: From the left of decimal point, converting every 4 (because 16 = 2^4) binary numbers to a hex number
• Decimal part: From the right of decimal point, converting every 4 (because 16 = 2^4) binary numbers to a hex number

Like binary number `100001001110.110101`

Integer part:

 ``````1 2 `````` ``````1000 0100 1110 8 4 E ``````

Decimal part:

 ``````1 2 `````` ``````1101 0100 D 4 ``````

The result: `0x84E.D4`

#### Hex to binary

Just reverse the process above, converting every hex number to 4 binary numbers

Like hex number `2FA.DE`

 ``````1 2 `````` ``````2 F A . D E 0010 1111 1010 . 1101 1110 ``````

The result: `1011111010.1101111`

### Minimum length for conversion

If a decimal integer X converts to N based, how many length to present the number after the conversion?

The maximum integer number which length is n and is N based number can present is `N^n - 1`
So if there is a situation exists that `N^(n-1) - 1 < X <= N^n - 1`, which means that X needs to be presented in n-length after conversion.

For example: a decimal number `144` converts to binary number, since `2^7 - 1 < 144 < 2^8 - 1`
`144` needs a 8-length binary number to present at least.

Hosted by Cloudflare
Built with Hugo
Theme Stack designed by Jimmy