In this tutorial, i will show you how to Binary (or bits) representation of integer in c++.

All data in a computer program are stored as bits in memory as numbers 0 and 1; so i am about to show you how bit integers are represented as bits because there are many uses for bits operations and manipulation in computer programming.

### Signed Bit Representation

The bit representation of a number e.g 43 is represented in its binary form:

But in programming, the length of the bit representation depends on the data type. Int in c++ is usually represented by 4bytes which is 32bits (if yours is 2bytes, then you are dealing with 16bits); so any variable declared as int in c++ consist of 32bits. Since the binary representation of the number 43 is 101011, we can represent it as a 32bit representation thus:

filling all the space to the left with 0’s for a non negative signed integer and 1’s for negative signed integers.

- The bit representation of a number is either signed or unsigned.
- A signed representation of n bits can contain any integer between -2
^{n-1}to 2^{n-1}-1. - int in c++ is a signed type and contains 32bits (i.e 4bytes depending on your compiler) and can contain any integer between -2
^{31}to -2^{31}-1 which is between -2147483648 and 2147483647. - When the first bit in a signed representation start with 1 – the number is a negative number; when it starts with 0, it is a non negative number.
- The opposite number of a number is calculated by either the method called
**two’s complement binary**or**Signed magnitude**

### Two’s complement binary

In this method, the negative number of a number can be calculated by first inverting all the bits in the number and then increasing the number by 1.

For example the bit representation of 43 in c++ is: 00000000000000000000000000101011; thus the bit representation of -43 using two’s compliment binary is: 11111111111111111111111111010101.

Notice that 101011 has been inverted to 010100 and then incremented by 1 therefore becoming 010101. And of course all of the 0s’ to the left of 101011 has been inverted to 1s’. Hence we get the binary 11111111111111111111111111010101 at the end.

### Signed magnitude

In this method, the first digit of the binary representation is the sign while the rest of the binary numbers are the magnitude.

For example the bit representation of the 43 for the type int will be: 000000000000000000000000000101011

Count the numbers now and you will see that it is 33 in number instead of 32; the first 0 shows that it is a non negative number.

To represent -43, we would have: 100000000000000000000000000101011 and the 1 before the rest of the binary is the sign indicating that it is a negative integer.

So for signed magnitude, 0 represent non negative number, while 1 represent negative number.

### Unsigned Bit Representation

- An unsigned representation can only contain non-negative numbers – meaning it should start with 0 and cannot be inverted like in the case of negative bits representation of signed.
- The unsigned int representation in c++ can contain any integer between 0 and 2
^{n}-1 where n is the number of bits. - There is a connection between signed representation and unsigned representation. A number -x in signed representation equals the number 2
^{n}-x in unsigned representation. For e.g -43 equals 2^{32}-43 in unsigned

### Additional Information

- If a number is larger than the upper bound of the bit representation, the number will overflow.
- In a signed representation, the next number after 2
^{n-1}-1 is -2^{n-1}(i.e the lower bound of signed int); and for unsigned representation, the next number after 2^{n-1}is 0 (the lower bound of unsigned representation). - The largest number that can be stored in an int variable is 2147483647; if you increment this by 1, it will overflow and become -2147483648

### C++ Program for bits Representation

```
int main(){
//A number -x in a signed operation equals 2 raised
//to n minus x in an unsigned representation, where
//n is 4bytes(memory allocated for int)*8 i.e 32bits
int x = -43;
unsigned int y = x;
cout<<x<<"\n";//prints -43
cout<<y<<"\n\n";//prints 4294967253
//if a number is larger than the upper bound of the
//bit representation, the number will overflow. In
//a signed representation, the next number after
//2 raised to power n minus 1 is -2 raised to n - 1
x = 2147483647;
cout<<x<<"\n";//2147483647
x++;
cout<<x<<"\n\n";//-2147483648
//print the bit representation for an int number x
int i;
x = 43;
for(i=31;i>=0;i--)
(x&(1<<i))?cout<<"1":cout<<"0";
cout<<endl;
}
```