5

Please view the following code and help me to understand it

int a=1;
int b=~1;
printf("%d",b);

Output is:

 -2

So this says that 1=(00000001) when undergoes ~ produces (11111110) which is 2's complement of number 2 and hence -2 is the answer. So 100 will always be assumed to be -4 but not 4 ?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
anime
  • 118
  • 10
  • A negative number will always have the uppermost bit set. So if your bytes are more than 3 bits long, 100 will always be 8, not -4. – Mad Physicist Dec 25 '17 at 00:35
  • 4
    Not every computer uses two's complement technically speaking, but it is very rare to find one that doesn't these days. – Mad Physicist Dec 25 '17 at 00:36
  • It sounds like you are asking for a tutorial in elementary binary encoding. – Mad Physicist Dec 25 '17 at 00:37
  • @MadPhysicist In a general architecture 1 byte will be used so the result (11111110) will assume 1 as MSB and rest as data bit and convert it to original number,right? – anime Dec 25 '17 at 00:38
  • So @MadPhysicist suppose i have a 3 bit register and i store 100 there, so my computer will understand it as -4 right? – anime Dec 25 '17 at 00:39
  • 1
    Not quite. The MSB is both a flag and part of the data. Also, while it is generally 8 bits, a byte can be any number of binary digits. – Mad Physicist Dec 25 '17 at 00:40
  • Yes 100 in a 3-bit register is -4 – Mad Physicist Dec 25 '17 at 00:41
  • @MadPhysicist 100 is 4. 1000 is 8. So with more than 3 bits 100 will be 4, not 8 as you claim. – Tom Karzes Dec 25 '17 at 00:41
  • @TomKarzes I agree with your point that 100 in a 3bit form will be 4 and not -4. – anime Dec 25 '17 at 00:42
  • @Tom. My mistake. Yes. A 3 bit register holds signed values from -4 to +3 in two's complement. – Mad Physicist Dec 25 '17 at 00:42
  • @anime. It depends on whether you apply signed or unsigned operations to the register. – Mad Physicist Dec 25 '17 at 00:44
  • So if signed operations are allowed which is in general allowed for a computer by default I can't store 4? – anime Dec 25 '17 at 00:46
  • I think a better initial hypothesis: "computers *use a fixed number of bits* for native integer types": 8, 16, 32, 64, etc. There is no native "3-bit value" which "uses 3 bits" (it can be shoved in *other* integer value widths, sure). Compare this with what has been presented. – user2864740 Dec 25 '17 at 00:50
  • 1
    @anime. I've never heard of a general purpose machine that didn't allow both signed and unsigned operations. A register is interpreted as whatever the operation needs. If you call unsigned addition, 100 in a 3-bit register is 4. If you use signed addition, it's -4. *you* have to decide which you want. 100 is just a collection of bits in memory and the processor does whatever you tell it to. – Mad Physicist Dec 25 '17 at 00:53
  • @user2864740. That is useful. I think that OP is having trouble understanding the difference between signed and unsigned and how it is reflected in memory. – Mad Physicist Dec 25 '17 at 00:55
  • @anime. I had trouble understanding your question initially. It's one of those times where it's hard to know the right way to ask without knowing all the terms and concepts up front. It's Christmas, so I'll remove my close vote and I'll draft you an answer shortly. – Mad Physicist Dec 25 '17 at 01:14
  • always? obviously not. There were computers that use other signed formats, and C and C++ standards also allow that. [Is there any existing CPU implementation which uses one's complement?](https://superuser.com/q/1137182/241386), [Are there any non-twos-complement implementations of C?](https://stackoverflow.com/q/12276957/995714) – phuclv Dec 25 '17 at 03:07
  • One thing to note is that GCC is pretty likely the compiler with the most target platforms in existence and [*GCC supports only two's complement integer types, and all bit patterns are ordinary values.*](https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/Integers-implementation.html) - so most likely the other formats you'll never meet. – Antti Haapala -- Слава Україні Dec 25 '17 at 06:49

2 Answers2

2

Does computer always follow 2's complement method to represent negative number?

No.

Some computers used 1's compliment (where ~1 == -0), some used "sign and magnitude" (where ~1 == -127), some use "bias" (where there signed value is "unsigned value - bias" and where ~1 == 127). For integers these are all relatively rare now.

Something that isn't rare is standard (IEEE-754) floating point formats; which are a glorious combination of "sign and magnitude" (used for the significand) and "bias" (used for the exponent).

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • Given the question is tagged C, you're probably better off limiting yourself to two's complement, ones' complement, and sign/magnitude. No other options are allowed by the standard. And note the positioning of the apostrophe, it's important :-) – paxdiablo Dec 25 '17 at 08:22
  • To clarify "relatively rare", does the total number of conforming C99 or C11 implementations intended for practical use that have *ever* employed anything other than two's-complement exceed zero? – supercat Aug 01 '18 at 19:34
0

You can only define two's complement numbers when you already have decided exactly how many bits hold the number. Do you have a three-bit signed integer type somewhere where you are storing the bits 100? If so, then 100 would be interpreted as -4.

If you store it in a larger integer type, we would normally assume the other bits to the left of the 1 are all 0s (since otherwise you should have shown what they were) and the value would be positive 4.

By the way, it would be very unusual nowadays to find a compiler that has a C or C++ int type that is only 8 bits long like the one in the question. (OK, "unusual" is an understatement--as a comment notes, the standard doesn't allow this, and as far as I know it has never been considered legitimate to have a plain int type as shown in the question that could be stored in fewer than 16 bits. You can declare a signed integer bit-field of just 3 bits within a struct, but the syntax for that is quite different from what the question shows.) So it is not really correct even to interpret int b=~1; as storing the bit pattern 11111110; the actual bit pattern would have at least 16 bits, and most of us in recent years have only seen it compiled as 32 bits.

David K
  • 3,147
  • 2
  • 13
  • 19
  • So suppose i have a 3 bit register and i store 100 there, so my computer will understand it as -4 right? – anime Dec 25 '17 at 00:40
  • Yes, if you can find such a thing. You might try a bit field (https://learn.microsoft.com/en-us/cpp/c-language/c-bit-fields) declared as a signed int -- I've never tried it myself, though. – David K Dec 25 '17 at 00:44
  • @anime. Your computer does not understand anything. It has some operations you can trigger. Some of these will be signed, and will treat 100 as -4. Others will be unsigned and will treat 100 as +4. Your job is to show the necessary understanding and apply the correct operations. When you use a C++ compiler, you do this by declaring the correct type and by casting when you need to. – Mad Physicist Dec 25 '17 at 00:49
  • @DavidK I have a similar problem here suppose for a 3 bit register if I do '2<<1' so my answer will be then '010<<1' produce '100' which will be -4 right? – anime Dec 25 '17 at 00:50
  • The C standard mandates at least 16 bits (technically, it mandates that it be big enough for ±32767). – aaaaaa123456789 Dec 25 '17 at 02:40
  • @aaaaaa123456789 in **C** you can define a signed bitfield of width `3` and if it is in 2's complement, and without trap values, it can store values -4 ... 3 – Antti Haapala -- Слава Україні Dec 25 '17 at 06:53
  • @Haapala I know; I was replying to the last paragraph of the answer. Should've been clearer; my bad. – aaaaaa123456789 Dec 25 '17 at 22:56
  • @AnttiHaapala I understood the comment by aaaaaa123456789 as it was intended; both that comment and my last paragraph were about `int` as declared in the question (not a bitfield). The last paragraph is really about how the example `int b=~1;` is interpreted in the question, not about how to interpret 100. – David K Dec 25 '17 at 23:42