# ~ question required

PETER CARTER

Ranch Hand

Posts: 70

Mohit Agarwal

Ranch Hand

Posts: 88

Tom Tolman

Ranch Hand

Posts: 83

posted 12 years ago

An int is a 32 bit value represented as 2's complement. Some simple values to MEMORIZE:

1 = 0x00000001 = 000000000000000000000000000001

-1 = 0xffffffff = 111111111111111111111111111111

0 = 0x00000000 = 000000000000000000000000000000

Now 6 is equal to 0x00000006 or

00000000000000000000000000000110 (4 + 2)

Invert this, and you get

11111111111111111111111111111001

What is this in decimal? Well, if you don't want to memorize an equation, walk through the negative numbers

00000000000000000000000000000000 = 0

11111111111111111111111111111111 = -1

11111111111111111111111111111110 = -2

11111111111111111111111111111101 = -3

11111111111111111111111111111100 = -4

11111111111111111111111111111011 = -5

11111111111111111111111111111010 = -6

11111111111111111111111111111001 = -7

Or if you prefer, you can memorize an equation- do you see the relationship between the above numbers and the positive numbers?

To go from negative to positive, you invert the bits and add one

To go from positive to negative, you invert the bits and add one (1 inverted is 11111111111111111111111111111110, and you need to add one to get to 11111111111111111111111111111111)

So now you are inverting the bits on the number six. You have not added one. If you did add one, you would have -6. So -6 missing 1 is -7.

1 = 0x00000001 = 000000000000000000000000000001

-1 = 0xffffffff = 111111111111111111111111111111

0 = 0x00000000 = 000000000000000000000000000000

Now 6 is equal to 0x00000006 or

00000000000000000000000000000110 (4 + 2)

Invert this, and you get

11111111111111111111111111111001

What is this in decimal? Well, if you don't want to memorize an equation, walk through the negative numbers

00000000000000000000000000000000 = 0

11111111111111111111111111111111 = -1

11111111111111111111111111111110 = -2

11111111111111111111111111111101 = -3

11111111111111111111111111111100 = -4

11111111111111111111111111111011 = -5

11111111111111111111111111111010 = -6

11111111111111111111111111111001 = -7

Or if you prefer, you can memorize an equation- do you see the relationship between the above numbers and the positive numbers?

To go from negative to positive, you invert the bits and add one

To go from positive to negative, you invert the bits and add one (1 inverted is 11111111111111111111111111111110, and you need to add one to get to 11111111111111111111111111111111)

So now you are inverting the bits on the number six. You have not added one. If you did add one, you would have -6. So -6 missing 1 is -7.