# Representation of Negative Binary Numbers & 2’s Complement

Representation of Negative Binary Numbers

ðŸ’¡**Most Significant Bit**

The leading 2 in the decimal number 231 is the most important digit or most significant digit. Compared to the other two digits, the leading 2 determines the greatest part of the number’s numerical value, as it represents the hundreds in the number.

Take, for instance, the binary number **11100111 **(231 in decimal), which can be sent as a string of data across a network in one of two ways:

starting from left to right, or starting right to left.

Most Significant Bit First and Least Significant Bit First are two common names for these two orderings.

ðŸ’¡**Least Significant Bit**

The rightmost bit in a string is the least significant bit. It is called that because it has the least effect on the value of the binary number, in the same way as the unit digit in a decimal number has the least effect on the number’s value.

The LSB also decides whether an even or odd number is delivered.

For example- The number 11100111 is an odd number since its LSB (1) is an odd number.

ðŸ’¡**Importance of Most Significant Bit (MSB)**

In the Binary number system, we treat MSB as a signed bit.

The MSB can also be used to denote the sign bit of a binary number in its one’s or two’s complement notation, with 1 meaning it’s a negative number, and 0 meaning it’s a positive number.

ðŸ’¡**One’s complement & two’s complement**

A binary number can be converted to its one’s complement by simply flipping all of its digits.

```
Number = 1 0 0 0 1 0
1'S Complement = 0 1 1 1 0 1 (Flipping all bits)
```

In order to calculate a binary number’s two’s complement, we first determine its one’s complement (by swapping its digits like before), and then add 1 to it.

```
Number = 1 0 0 0 1 0
1'S Complement = 0 1 1 1 0 1 (Flipping all bits)
Add 1 = 0 0 0 0 0 1
+ ____________
2'S Complement 0 1 1 1 1 0
```

In basic calculus, these two’s complements will behave like the negative number of the starting bit sequence.

Return negative number of given number.

```
Given Number = 45
Binary representation = 1 0 1 1 0 1
Int Size = This is the Int value and Int has 4 bytes (4 * 8 = 32-bit).
8 Bit representation of 45 = 0 0 1 0 1 1 0 1
Flip all values = 1 1 0 1 0 0 1 0 Â Â Â (1's Complement)
Now add 1 = 0 0 0 0 0 0 0 1
+ _____________________
Result = 1 1 0 1 0 0 1 1 Â (2's complement)
Decimal Value Of result = 211
MSB of 211 is 1, means it will consider as negative digit.
Result = 1 1 0 1 0 0 1 1
= -128 64 32 16 8 4 2 1
Final = -45.
```

ðŸ’¡**What is the size of Int?**

In 16-bit operating systems, the int type is usually 16 bits or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits or 4 bytes.

ðŸ’¡**64-bit Standard sizes of Data types**

**1 byte = 8 bits**

```
Name Length
char 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
long double 16 bytes
```

https://byjus.com/maths/binary-subtraction/

Find Factors of a Number â€“ JavaScript Program

Fibonacci series in JavaScript | Four different ways

LeetCode â€“ Coin Change Problem | Dynamic Programming | JavaScript

Representation of Negative Binary Numbers