## Conversion from binary to hex

Given a binary number, group its digits in groups of 4, then replace according to the below table:

Binary | Hex |
---|---|

0001 | 1 |

0010 | 2 |

0011 | 3 |

0100 | 4 |

0101 | 5 |

0110 | 6 |

0111 | 7 |

1000 | 8 |

1001 | 9 |

1010 | A |

1011 | B |

1100 | C |

1101 | D |

1110 | E |

1111 | F |

### Proof

## Conversion from binary to octal

Similarly, it’s possible to convert a binary number to octal by grouping the digits in groups of 3:

## Mnemonics

For any base , equals 1 followed by zeros:

equals times the digit :

has the same representation in any base:

Multiplying by equates to shifting the digits one place to the left:

Integer division by equates to shifting the digits one place to the right:

## Numeric complements

### Radix complement

Radix complement of an n-digit number in radix is the difference between and .

If is a n-digit number, then radix complement, , is , e.g.

### Diminished radix complement

Diminished radix complement is radix complement minus 1,

The advantage of using over is its simplicity to calculate: just replace each digit for its corresponding , e.g.

In a binary representation, it’s even easier as the calculation of the is equivalent to flipping zeros and ones:

(1)

#### Proof

(2)

Resulting:

In binary, the radix complement is called the **two’s complement **and the diminished radix complement the **ones’ complement**.

## Negative numbers

Radix and diminished radix complements are used to represent negative numbers. Given a range of unsigned numbers , the subinterval corresponds to positive numbers and the subinterval to negative numbers. Any represents the negative value of its radix or diminished radix (depending on the convention used) complement. This way subtractions can be handled as additions without needing to deal with the sign of the negative numbers.

For instance, when using the radix complement in the interval , the negative values are:

and therefore, the numbers represented by the elements of are:

If instead we use diminished radix complement, the negative values are:

and the numbers that can be represented are:

From the above results, it follows that given the interval of unsigned numbers , the corresponding interval of signed numbers is:

- in radix complement
- in diminished radix complement

### Arithmetic with radix complement

Let’s consider now how to add/subtract the numbers in the range when using radix complement to represent negative values.

Given , the result is out of the range of the interval. We need to remove the overflowing digit to get the expected value .

In this other example .

Based on the above examples, we can infer the following rules for the arithmetic of negative numbers with radix complement:

- replace the subtrahend with its complement
- do the sum
- if the result overflows, remove the overflowing digit
- otherwise, replace it with the negative of its complement

Let’s prove more formally the above rules derived intuitively:

if ,

where (and subtracting is equivalent to dropping the overflowing bit).

On the other hand, if , then and, after re-arranging the terms, the above formula becomes:

#### Binary system examples

Range of signed 2-byte integers:

### Arithmetic with diminished radix complement

Let’s take the same examples from the previous section:

, the result is out of the range of the interval. We need to remove the overflowing digit and add 1, resulting in .

, similarly to what we did in the previous section, we need to get the complement of the result to obtain the expected value .

Again, we can infer the following rules for the arithmetic of negative numbers with diminished radix complement:

- replace the subtrahend with its complement
- do the sum
- if the result overflows, remove the overflowing digit and add 1
- if not, replace it with the negative of its complement

Here’s the demonstration:

if ,

where (and subtracting is equivalent to dropping the overflowing bit).

If , then and the above formula, after re-arranging the terms, becomes:

#### Binary system examples

Range of signed 2-byte integers:

## Invert operator

The unary (invert) operator yields the bitwise inversion of its integer argument (in the binary system that is equivalent to calculate the diminished radix complement).

However, the number represented by the result is not well defined as it depends on the number of bits used to represent the original value:

But if we calculate the radix complement of :

the resulting value is the same, irrespective of the number of bits. In other words, is the radix complement of and therefore can be considered to represent .

Based on the above discussion, we can define as: