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
10
votes
2 answers

Adding two signed or unsigned integers

It has been a long time since I last programmed at the bits and bytes level and wanted to confirm something I seem to remember from those days: Say I have two integers of equal length (1, 2, 4, 8 bytes; it doesn't matter), and I add them up: does…
Eduardo
  • 8,362
  • 6
  • 38
  • 72
10
votes
3 answers

Bitwise operations and shifts

Im having some trouble understanding how and why this code works the way it does. My partner in this assignment finished this part and I cant get ahold of him to find out how and why this works. I've tried a few different things to understand it,…
9
votes
3 answers

represent negative number with 2' complement technique?

I am using 2' complement to represent a negative number in binary form Case 1:number -5 According to the 2' complement technique: Convert 5 to the binary form: 00000101, then flip the bits 11111010, then add 1 00000001 => result: 11111011 To make…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
9
votes
3 answers

Is plain char usually/always unsigned on non-twos-complement systems?

Obviously the standard says nothing about this, but I'm interested more from a practical/historical standpoint: did systems with non-twos-complement arithmetic use a plain char type that's unsigned? Otherwise you have potentially all sorts of…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
9
votes
2 answers

Two's complement of long integer

I want to do some long integer math (128 bit) with Intel I64 Assembler and need to create a 2's complement. Let's say that my positive value is in RDX:RAX. 2's complement is done by "flip the bits and add 1". So the most naive implementation is (4…
Rolf
  • 730
  • 6
  • 14
9
votes
3 answers

Using -1 as a flag value for unsigned (size_t) types

I was using -1 as a flag value for a function whose return type is size_t (an unsigned type). I didn't notice it at first, particularly because it wasn't causing any errors in my code (I was checking it with x == -1, not x < 0). Are there any subtle…
dspyz
  • 5,280
  • 2
  • 25
  • 63
9
votes
4 answers

Detection of Negative Binary

Today in class my Computing teacher was explaining to us (or attempting to explain) how to write a negative binary number using Two's Complement. My question is this: How does the end user determine the difference between 11101100 being 236 and -20?…
Kezz
  • 1,680
  • 3
  • 19
  • 37
8
votes
1 answer

Questions about C++20 two's-complement proposal R4

I am reading revision 4 of the two's-complement proposal (adopted by C++20), and I have some questions. In the introduction, it says: Status-quo Signed integer arithmetic remains non-commutative in general (though some implementations may guarantee…
Nemo
  • 70,042
  • 10
  • 116
  • 153
8
votes
2 answers

Significance of x & (-x) in 2's Complement?

Where '-' denotes negative x, and '&' denotes bitwise AND. The numbers are in 8-bit 2's complement in a program and I can't seem to find the correlation between inputs and outputs. 8 & (-8) = 8 7 & (-7) = 1 97 & (-97) = 1 So possibly the…
ThoseKind
  • 754
  • 2
  • 13
  • 22
8
votes
4 answers

What is the difference between literals and variables in C (signed vs unsigned short ints)?

I have seen the following code in the book Computer Systems: A Programmer's Perspective, 2/E. This works well and creates the desired output. The output can be explained by the difference of signed and unsigned representations. #include int…
8
votes
1 answer

Guaranteeing negative result when left shifting a negative number in two's complement?

Assuming a negative binary number is represented in two's complement, how can we guarantee the sign is preserved? Let's say we represent a decimal number -5 in four bits: 1011, and want to left-shift one place to multiply by 2: 1011 << 1 This…
8
votes
4 answers

How to take twos complement of a byte in c++?

I am looking at some C++ code and I see: byte b = someByteValue; // take twos complement byte TwosComplement = -b; Is this code taking the twos complement of b? If not, What is it doing?
amalgamate
  • 2,200
  • 5
  • 22
  • 44
7
votes
10 answers

Why does -INT_MIN = INT_MIN in a signed, two's complement representation?

I still haven't found a reason why the lowest signed negative number doesn't have an equivalent signed positive number? I mean in a 3 digit binary number for simplicity 100 is -4? but we can't have a positive 4 in signed format because we can't. It…
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
7
votes
1 answer

Floating Point Monotonic Property

I was reading a book (CSAPP) . In the book it is written that- floating point addition satisfies the following monotonicity property : if a>=b then (x + a) >= (x+b) for any value a,b and x other than NaN. This property of real (and integer)…
7
votes
2 answers

Adding and subtracting two's complement

Using six-bit one's and two's complement representation I am trying to solve the following problem: 12 - 7 Now, i take 12 in binary and 7 in binary first. 12 = 001100 - 6 bit 7 = 000111 - 6 bit Then, would I then flip the bit for two's…
user249375