# Binary Number System and Its Representation

In mathematics and digital electronics, a binary number is expressed in the base-2 number system, using only two symbols: "0" and "1". Another number system that became famous after the decimal is the binary number system, which has only two digits, 0 and 1.

In mathematics and digital electronics, a binary number is expressed in the base-2 number system, using only two symbols: "0" and "1".

## What is the binary number system?

Another number system that became famous after the decimal is the binary number system, which has only two digits, 0 and 1.

If a number system has n digits, we say that the base of the number system is n. So the binary number system can also be called the base-2 number system.

### Why does a computer understand binary?

The simplest explanation would be that a computer is an electrical device, and all electrical devices understand electrical signals, which have only two states.

### Example

If we have an input wire to this machine, there are only two possible states for this wire: either the current is flowing through this wire, or it is not flowing through this wire. If the current is flowing, we say that the state of this wire is signalled. And we say that the signal state corresponds to 1.

If the current is not flowing, it is not signalled. The not signal state corresponds to 0. So, 1 and 0, in binary, translate to a signal or non-signal in an electrical device, and we can have multiple wires or inputs to represent multiple ones and zeros.

### Powers of 2

Power of two Binary Decimal Value
2^0 0001 1
2^1 0010 2
2^2 0100 4
2^3 1000 8
2^4 0001 0000 16
2^5 0010 0000 32
2^6 0100 0000 64
2^7 1000 0000 128
2^8 0001 0000 0000 256
2^9 0010 0000 0000 512
2^10 0100 0000 0000 1,024

The powers of 2 are increasing, so the bits go from right to left based on the decimal value given as input. All other left bits will be `0`.

For example:

`125` can be represented as `01111101` in the computer binary system. Anything in computer language gets converted into a binary number system.

## What do binary numbers represent?

In mathematics and digital electronics:

• A binary number is expressed in the base-2 or binary number system.
• It uses only two symbols: typically “0” (zero) and “1” (one).

The base-2 number system is a positional notation with a radix of 2. Each digit is referred to as a bit.

### Binary counting

Binary counting follows the same procedure, except only the symbols `0` and `1` are available. Thus, after a digit reaches 1 in binary, an increment resets it to 0 but also causes an increment of the next digit to the left.

0000,
0001, (rightmost digit starts over, and next digit is incremented)
0010, 0011, (rightmost two digits start over, and next digit is incremented)
0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented)
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111,…

### Binary to decimal conversion

In the binary system, each digit represents an increasing power of 2, with the rightmost digit representing 20, the next representing 21, then 22, and so on. The value of a binary number is the sum of the powers of 2 represented by each “1” digit. For example, the binary number 100101 is converted to decimal form as follows:

1001012 = [ ( 1 ) x 25 ] + [ ( 0 ) x 24 ] + [ ( 0 ) x 23 ] + [ ( 1 ) x 22 ] + [ ( 0 ) x 21 ] + [ ( 1 ) x 20 ]

1001012 = [ ( 1 ) x 32 ] + [ ( 0 ) x 16 ] + [ ( 0 ) x 8 ] + [ ( 1 ) x 4 ] + [ ( 0 ) x 2 ] + [ ( 1 ) x 0 ]

1001012 = 3710

### Decimal to binary representation

Below is the 32-bit binary representation.

``5 -> 00000000 00000000 00000000 00000101``

### Representing decimals & ASCII in binary

A computer only understands byte-code made of 0’s and 1’s. We must represent every decimal character as binary digits so a computer can understand our instructions.

#### Decimal numbers in binary (8-bit representation)

Each software programming language uses its pre-defined sizes for primitive data types. So, let’s represent the rightmost 8 bits (1 byte) in binary.

Decimal Number 8-bit binary representation
0 0000 0000
1 0000 0001
2 0000 0010
3 0000 0011
4 0000 0100
5 0000 0101
6 0000 0110
7 0000 0111
8 0000 1000
9 0000 1001
10 0000 1010

#### ASCII - Binary character table

Alphabets in binary (capital letters & lowercase letters)

Letter ASCII Code Binary Letter ASCII Code Binary
a 097 01100001 A 065 01000001
b 098 01100010 B 066 01000010
c 099 01100011 C 067 01000011
d 100 01100100 D 068 01000100
e 101 01100101 E 069 01000101
f 102 01100110 F 070 01000110
g 103 01100111 G 071 01000111
h 104 01101000 H 072 01001000
i 105 01101001 I 073 01001001
j 106 01101010 J 074 01001010
k 107 01101011 K 075 01001011
l 108 01101100 L 076 01001100
m 109 01101101 M 077 01001101
n 110 01101110 N 078 01001110
o 111 01101111 O 079 01001111
p 112 01110000 P 080 01010000
q 113 01110001 Q 081 01010001
r 114 01110010 R 082 01010010
s 115 01110011 S 083 01010011
t 116 01110100 T 084 01010100
u 117 01110101 U 085 01010101
v 118 01110110 V 086 01010110
w 119 01110111 W 087 01010111
x 120 01111000 X 088 01011000
y 121 01111001 Y 089 01011001
z 122 01111010 Z 090 01011010
Bit ManipulationData Structures and Algorithms

### Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Members Public

## Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Members Public

## Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Members Public

## Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things