Storing negative integers in memory

Hello, I have a little doubt in 2’s complement. Suppose we want to store -5 in a variable(memory). As the datatype int consumes 4 bytes(32 bits) of memory hence 5 is written as:

00000000000000000000000000000101

Using 2’s complement we will write -5 as:

10100000000000000000000000000001

Is this right or is there something that I am missing or getting confused into ?

1111 1111 1111 1111 1111 1111 1111 1011

Negate all bits and then add 1

but how will it happen ??

@yashsharma4304

Ok so negating a bit means convert 1 to 0 and 0 to 1. Right?

Yes @yashsharma4304
Correct

After neagating this
1111 1111 1111 1111 1111 1111 1111 1010

Now add 1
1111 1111 1111 1111 1111 1111 1111 1011

Ok now I understand what the flipping of a bit means. Thank u for resolving my doubt.

1 Like