-4

I'm doubt why we uses -1 as 11111111 in signed int, not 10000001. because as we learned first bit is use to specify the sign and rest of the seven bits are for the value. So in what reason designers decide to use -1 as

[ first bit = 1 , Value by seven bits=127 ] but not [ first bit = 1 , Value by seven bits=1 ]

Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147

1 Answers1

1

C and C++ allow both sign-and-magnitude (with -1 like 10000001) and two's complement form (with -1 like 11111111).

In addition both languages allow one's complement form (with -1 like 11111110).

However, on most all extant platforms two's complement form is used. One main advantage (the “why” you ask about) is that the values are the same as you get with wrap-around for unsigned arithmetic, which is what you get with unsigned in C and C++. Put another way, if you interpret the same bits as an unsigned value, then it differs from the negative value by exactly 2n, where n is the number of value representation bits.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • You can read this article in Medium that I created that goes step by step on how this works like this: https://medium.com/@jeremythen16/signed-vs-unsigned-integers-binary-representation-overflows-pitfalls-109bc1ef6ef0 – Jeremy Then Jul 12 '22 at 13:46