## Binary numbers are a base 2 numeral system.

A binary number is a number expressed in the base-2 numeral system, which uses only two symbols: 0 and 1. Each digit is referred to as a bit. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used by almost all modern computers and computer-based devices.

### Basics

**Bit**: The smallest unit in computers. It is either a 1 or a 0.**Nibble**: Half a byte, or 4 bites.**Byte**: 8 bits together form one byte, a number from 0 to 255. Bits in the byte are numbered starting from the right at 0.**Short**: Two bytes put together is 16 bits, forming a number from 0 to 65535. The low byte is the rightmost eight bits.**Hex Number**: A HEX number consisting of 4 numbers is 16-bit.

### Binary to Hexadecimal Conversion

Break down the binary value in chunks of 4, multiply each 1 by its equivalent value, either 8, 4, 2 or 1. Add the resulting numbers together to get the result. For example, the value 1100, or (8*1 + 4*1), is equal to C(decimal 12).

0 | 1 | 0 | 1 | Binary |

8 | 4 | 2 | 1 | Values |

4 | 1 | Result: 5 |

### Hexadecimal to Binary Conversion

0 | 0000 | 4 | 0100 | 8 | 1000 | C | 1100 |

1 | 0001 | 5 | 0101 | 9 | 1001 | D | 1101 |

2 | 0010 | 6 | 0110 | A | 1010 | E | 1110 |

3 | 0011 | 7 | 0111 | B | 1011 | F | 1111 |

### Arithmetic

**Addition**

input | ADD | output | carry |

0 | 1 | 1 | 0 |

0 | 0 | 0 | 0 |

1 | 1 | 0 | 1 |

1 | 0 | 1 | 0 |

**Subtraction**

input | SUB | output | borrow |

0 | 1 | 1 | 1 |

0 | 0 | 0 | 0 |

1 | 1 | 0 | 0 |

1 | 0 | 1 | 0 |

**Multiplication**

input | MUL | output |

0 | 1 | 0 |

0 | 0 | 0 |

1 | 1 | 1 |

1 | 0 | 0 |

### Bit Masks

**AND**, or "both", sets individual bits to 0. AND is useful for masking bits, for example, to mask the high order bits of a value AND with $0F: $36 AND $0F = $06.

input | AND | output |

0 | 1 | 0 |

0 | 0 | 0 |

1 | 1 | 1 |

1 | 0 | 0 |

**ORA**(OR), or "either one or both", sets individual bits to 1. OR is useful for setting a particular bit, for example, $80 OR $08 = $88

input | ORA | output |

0 | 1 | 1 |

0 | 0 | 0 |

1 | 1 | 1 |

1 | 0 | 1 |

**EOR**(XOR), or "one or the other but not both", inverts individual bits.

input | EOR | output |

0 | 1 | 1 |

0 | 0 | 0 |

1 | 1 | 0 |

1 | 0 | 1 |

### Rotate

**ROL** rotate one bit left to multiply by 2, and **ROR** rotates one bit right to divided by 2.

input | op | output |

0110(6) | ROL | 1100(C) |

0110(6) | ROR | 0011(3) |

### Signed Integers

If Bit 7 is not set (as in the first example) the representation of signed and unsigned numbers is the same. However, when Bit 7 is set, the number is always negative. For this reason Bit 7 is sometimes called the sign bit.

Binary | Unsigned | Signed |

0010 0011 | 35 | 35 |

1010 0011 | 163 | -93 |

1111 1111 | 255 | -1 |

1000 0000 | 128 | -128 |

### Add with bitwise in C

int Add(int x, int y) { /* Iterate till there is no carry */ while (y != 0) { /* carry now contains common */ /* set bits of x and y */ int carry = x & y; /* Sum of bits of x and y where at */ /* least one of the bits is not set */ x = x ^ y; /* Carry is shifted by one so that adding */ /* it to x gives the required sum */ y = carry << 1; } return x; }

*incoming(1)*: assembly