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
3
votes
1 answer

Can std::hex input format support negtive int16_t hex string with two's complement notation such as `ffff` for `-1`?

I would like to input a text string ffff to a int16_t, the value should be -1. Here is a simple test C++ program: #include #include #include int main() { int16_t num; std::cout << "Enter a hexadecimal…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
3
votes
2 answers

Going from signed integers to unsigned integers and vice versa in C++20

Before C++20 signed integers were not guaranteed to be two's complement. Now we have two papers proposing standardization of two's complement as the only representation: p0907 and p1236 and, if I understand correctly, one of them got merged into…
user3624760
3
votes
0 answers

C# Parsing Bytes as 2's complement from reading modbus Device

I read bytes from modbus device, and need to parse the bytes to integer. I try many ways to get the correct value, but no even close the target. ushort[] sourceBytes = new ushort[] { 0x1, 0x98bb };//from modbus int target = 79605;// near this…
Roger
  • 75
  • 4
3
votes
2 answers

Two's Complement Quick Way

The way to find Two's Complement of a binary number is: Let x ̄ = the logical complement of x. The logical complement (also called the one’s complement) is formed by flipping all the bits in the number, changing all of the 1 bits to 0, and vice…
keser
  • 2,472
  • 1
  • 12
  • 38
3
votes
1 answer

Two's complement function outputs wrong result for -1

I am generating the input for an FPGA program to use the trapezoidal integration method. Basically, the functions of interest here are the invert() and twos_comp() functions; the rest is just testing (creating a square wave signal, and then…
Auden Young
  • 1,147
  • 2
  • 18
  • 39
3
votes
3 answers

How do you detect 2's complement multiplication overflow?

In one of the books that I am reading, following function is used to determine 2's complement integer multiplication overflow. int tmult_ok(int x, int y) { int p = x*y; return !x || p/x == y; } While this works, how do I prove its correctness in…
3
votes
5 answers

2's complement in shellscript

I am trying to create a simple program in Shellscript(Bash) to calculate 2's complement binary numbers. As a test, I am only using an 8-bit binary value (00000100) but I keep getting a "Bad Substitution" error and I have no idea why. …
Mr Digs
  • 29
  • 1
  • 4
3
votes
2 answers

1's and 2's complement systems

I'm trying to understand the differences between these two systems and their impact on C programming. From what I've learned from Wikipedia: both systems are used to represent negative numbers one's complement applies bitwise NOT to negative number…
Mark
  • 1,751
  • 3
  • 14
  • 14
3
votes
1 answer

Why is 1's complement still used for encoding vector instructions?

In an answer, jww points out that 1's complement is still used in encoding vector instructions on intel architectures, and Ruslan clarifies that these instructions are being used more as auto-vectorization becomes common. Is there an advantage of…
Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
3
votes
1 answer

Interpret 16bit two's complement in javascript (nodejs)

Hello dear swarm intelligence, One of my current private projects is in the field of the internet of things, specifically LoRaWan and TTN. For easy data-handling I decided to use node-red which is a node-js based flow tool to process the received…
3
votes
2 answers

Bitwise carry applications

Call me naive but in this area I always struggled. So I was just browsing through the code for adding two numbers without + operator and bumped into this code: int Add(int x, int y) { // Iterate till there is no carry while (y != 0) { …
3
votes
4 answers

Wrapping my head around hardware representations of numbers: a hypothetical two's complement question

This is a super naive question (I know), but I think that it will make for a good jumping off point into considering how the basic instruction set of a CPU actually gets carried out: In a two's complement system, you cannot invert the sign of the…
tel
  • 13,005
  • 2
  • 44
  • 62
3
votes
2 answers

Binary Multiplication, 2's complement

I am trying to learn Binary Multiplication, 2's complement negative numbers. -10 x 3 I know there is a simple way of doing this. Like sign extension and initial partial product. -10 0110 twos complement x 3 x 0011 ---- ------ …
user249375
3
votes
1 answer

Why -2>>>1 equals 2147483647 in Java

-2's one's complement is 100000...01 -2's two's complement is 1000000...10 -2 >>> 1 According >>> definition left side shifts in 0 should be something like 01000......1, why becomes 0111111..11?
sthbuilder
  • 561
  • 1
  • 7
  • 22
3
votes
3 answers

2's Complement Error

I generally understand the concept of 2's complement. When converting a value to 2's complement for subtraction, simply negate each digit of the number to get 1's complement and add 1. When calculating the result of something like 2-2 in binary, you…
Orren Ravid
  • 560
  • 1
  • 6
  • 24