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
30
votes
5 answers

Why is the range of signed byte is from -128 to 127 (2's complement) and not from -127 to 127?

I read Why is the range of bytes -128 to 127 in Java? it says 128 is 10000000. Inverted, it's 01111111, and adding one gets 10000000 again so it concludes -128 is 10000000 so +128 cannot be represented in 2's complement in 8 bits, but that…
Anubha
  • 1,345
  • 6
  • 23
  • 35
26
votes
4 answers

Representation of negative numbers in C?

How does C represent negative integers? Is it by two's complement representation or by using the MSB (most significant bit)? -1 in hexadecimal is ffffffff. So please clarify this for me.
1s2a3n4j5e6e7v
  • 1,243
  • 3
  • 15
  • 29
24
votes
5 answers

Negative numbers are stored as 2's complement in memory, how does the CPU know if it's negative or positive?

-1 can be represented in 4 bit binary as (2's complement) 1111 15 is also represented as 1111. So, how does CPU differentiate between 15 and -1 when it gets values from memory?
MCG
  • 1,011
  • 1
  • 10
  • 21
23
votes
7 answers

Why byte b = (byte) 0xFF is equals to integer -1?

Why byte b = (byte) 0xFF is equal to integer -1? Ex: int value = byte b = (byte) 0xFF; System.out.println(value); it will print -1?
okami
  • 231
  • 1
  • 2
  • 3
22
votes
3 answers

How to represent a negative number with a fraction in 2's complement?

So I want to represent the number -12.5. So 12.5 equals to: 001100.100 If I don't calculate the fraction then it's simple, -12 is: 110100 But what is -12.5? is it 110100.100? How can I calculate this negative fraction?
Tom
  • 9,275
  • 25
  • 89
  • 147
21
votes
2 answers

how to do two complement multiplication and division of integers?

I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg:…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
20
votes
2 answers

What is the meaning of "producing negative zeroes" in a system that doesn't support it?

C17 6.2.6.2/4 says: If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<, and >> operators with operands that would produce such a value is undefined. If I have a 2's complement system, it does not support…
Lundin
  • 195,001
  • 40
  • 254
  • 396
20
votes
6 answers

How do I detect overflow while multiplying two 2's complement integers?

I want to multiply two numbers, and detect if there was an overflow. What is the simplest way to do that?
Lazer
  • 90,700
  • 113
  • 281
  • 364
18
votes
6 answers

Why Two's Complement?

I'm writing a tutorial to teach kids (ages 9 to 13) about programming. I started with computers themselves, they don't have that much to do with computer science, it's more about the process involved with a solution to a computational problem. With…
dvanaria
  • 6,593
  • 22
  • 62
  • 82
18
votes
2 answers

Ramifications of C++20 requiring two's complement

C++20 will specify that signed integral types must use two's complement. This doesn't seem like a big change given that (virtually?) every implementation currently uses two's complement. But I was wondering if this change might shift some…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
16
votes
2 answers

How is overflow detected in two's complement?

I see that when I subtract positive and negative number using two's complement I get overflows. For example, if I subtract 1 from 2 I get: 2 = 0010 1 = 0001 -> -1 = 1111 2 + (-1) -> 0010 + 1111 = 10001 So here the result has fifth left bit 10001 -…
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
16
votes
3 answers

-128 and 128 in 2's complement

In 2's complement, 0-127 is represented as 00000000 to 01111111. In case of negative numbers, we invert all bits in the unsigned representation and add 1 to get the 2's complement. (Reference:…
user720694
  • 2,035
  • 6
  • 35
  • 57
16
votes
1 answer

Detect one's or two's complement architecture in C++?

What is the most reliable way to detect whether the architecture uses one's or two's complement representation in C++?
Vincent
  • 57,703
  • 61
  • 205
  • 388
15
votes
5 answers

How to detect encodings on signed integers in C?

The ISO C standard allows three encoding methods for signed integers: two's complement, one's complement and sign/magnitude. What's an efficient or good way to detect the encoding at runtime (or some other time if there's a better solution)? I want…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
15
votes
10 answers

Two's complement conversion

I need to convert bytes in two's complement format to positive integer bytes. The range -128 to 127 mapped to 0 to 255. Examples: -128 (10000000) -> 0 , 127 (01111111) -> 255, etc. EDIT To clear up the confusion, the input byte is (of course) an…
Yehonatan
  • 1,172
  • 2
  • 11
  • 21
1
2
3
45 46