Questions tagged [twos-complement]

Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation.

A two's complement number follows the same principal as a pure binary number, except for the first bit: the most significant bit. If this bit is 0, the binary number is positive, if it's 1 then the number will be negative.

For example:

01110 = 8 + 4 + 2
      = 14

10010 = -16 + 2
      = -14

So 01110 is 14 in denary and 10010 is -14 in denary

683 questions
504
votes
24 answers

What is “two's complement”?

I'm in a computer systems course and have been struggling, in part, with two's complement. I want to understand it, but everything I've read hasn't brought the picture together for me. I've read the Wikipedia article and various other articles,…
235
votes
19 answers

Why prefer two's complement over sign-and-magnitude for signed numbers?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1…
Ray
  • 3,468
  • 8
  • 26
  • 27
153
votes
11 answers

~x + ~y == ~(x + y) is always false?

Does this code always evaluate to false? Both variables are two's complement signed ints. ~x + ~y == ~(x + y) I feel like there should be some number that satisfies the conditions. I tried testing the numbers between -5000 and 5000 but never…
Steve
  • 4,446
  • 7
  • 35
  • 50
91
votes
20 answers

Two's Complement in Python

Is there a built in function in python which will convert a binary string, for example '111111111111', to the two's complement integer -1?
Jim
  • 911
  • 1
  • 7
  • 3
91
votes
10 answers

How are integers internally represented at a bit level in Java?

I am trying to understand how Java stores integer internally. I know all java primitive integers are signed, (except short?). That means one less bit available in a byte for the number. My question is, are all integers (positive and negative) stored…
Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
75
votes
4 answers

why does long long 2147483647 + 1 = -2147483648?

Why doesn't this code print same number? : long long a, b; a = 2147483647 + 1; b = 2147483648; printf("%lld\n", a); printf("%lld\n", b); I know that int variable's maximum number is 2147483647 because int variable is 4 byte. But as I know, long…
Hoseong Jeon
  • 1,240
  • 2
  • 12
  • 20
67
votes
6 answers

Why is -(-2147483648) = - 2147483648 in a 32-bit machine?

I think the question is self explanatory, I guess it probably has something to do with overflow but still I do not quite get it. What is happening, bitwise, under the hood? Why does -(-2147483648) = -2147483648 (at least while compiling in C)?
Lesscomfortable
  • 823
  • 1
  • 6
  • 13
66
votes
2 answers

How memset initializes an array of integers by -1?

The manpage says about memset: #include void *memset(void *s, int c, size_t n) The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. It is obvious that memset can't be used to…
haccks
  • 104,019
  • 25
  • 176
  • 264
59
votes
2 answers

String.format() and hex numbers in Java

I'm trying to figure out why String.format() is behaving the way it does. Context: Systems programming class, writing an assembler. There is a 5 character hex field in the object file, which I am creating from a value. Tried using:…
Fitzoh
  • 776
  • 1
  • 6
  • 12
49
votes
10 answers

bitwise not operator

Why bitwise operation (~0); prints -1 ? In binary , not 0 should be 1 . why ?
Sawyer
  • 15,581
  • 27
  • 88
  • 124
42
votes
2 answers

sign changes when going from int to float and back

Consider the following code, which is an SSCCE of my actual problem: #include int roundtrip(int x) { return int(float(x)); } int main() { int a = 2147483583; int b = 2147483584; std::cout << a << " -> " << roundtrip(a)…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
38
votes
6 answers

why -3==~2 in C#

Unable to understand. Why output is "equal" code: if (-3 == ~2) Console.WriteLine("equal"); else Console.WriteLine("not equal"); output: equal
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
35
votes
9 answers

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); is true. I understand that integer in Java is 32 bit and can't go above 231-1, but I can't understand why adding 1 to its MAX_VALUE results in MIN_VALUE and not in some kind of…
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
31
votes
3 answers

Why are signed and unsigned multiplication different instructions on x86(-64)?

I thought the whole point of 2's complement was that operations could be implemented the same way for signed and unsigned numbers. Wikipedia even specifically lists multiply as one of the operations that benefits. So why does x86 have separate…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
30
votes
4 answers

Why not enforce 2's complement in C++?

The new C++ standard still refuses to specify the binary representation of integer types. Is this because there are real-world implementations of C++ that don't use 2's complement arithmetic? I find that hard to believe. Is it because the committee…
TonyK
  • 16,761
  • 4
  • 37
  • 72
1
2 3
45 46