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

What to_unsigned does?

Could someone please explain me how VHDL's to_unsigned works or confirm that my understanding is correct? For example: C(30 DOWNTO 0) <= std_logic_vector (to_unsigned(-30, 31)) Here is my understanding: -30 is a signed value, represented in bits…
2
votes
2 answers

XOR in java with negative bytes

byte b = -0b0101^-0b0100; System.out.println("Outputs "+b); //Outputs 7 Why does this output 7 (0b111) when I was expecting 1 (0b001)?
Klew
  • 23
  • 3
2
votes
1 answer

Binary Arithmetic using sign magnitude and two's complement

I am revising for an exam i have in a weeks time. I have an example question which i have answered but i am unsure if it is correct. Could someone please verify my answer for me and explain where iv gone wrong if it was incorrect. Here is the…
Volken
  • 255
  • 1
  • 4
  • 23
2
votes
1 answer

converting from uint8_t to long in C++

I am using a map type to mimic a memory i.e., map memory Where the long is the address and the uint8_t the byte of memory. Now I can write (signed) long types out without a problem (I think!): void Memory::writeLong(const long…
adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
2
votes
1 answer

How can I sign-extend a 5-bit value?

I have a unsigned integer (32 bit), in which I've stored an n-bit signed number (2's complement, so -16 ≤ x < 16, and the resultant unsigned value is in the range 0-15). How can I transform this efficiently into a signed interpretation using the…
Boris
  • 8,551
  • 25
  • 67
  • 120
2
votes
1 answer

Example for BASH two's complement with Hex values?

I have a routine that is collecting a hex value via SNMP. Here is a real collection from my bash script 08 01 18 00 FF FF. The value is base on expr $((16#${array[4]})) - $((16#${array[5]})) so the results are 0, how do I introduce two is…
justZito
  • 569
  • 1
  • 10
  • 19
2
votes
0 answers

Algorithm for 2's complement division

Came across two division algorithms (restoring and non-restoring) applicable to signed magnitude numbers. But couldn't apply it for numbers in 2's complement format. Are there any algorithms for 2's complement division?
user567879
  • 5,139
  • 20
  • 71
  • 105
2
votes
2 answers

What is the maximum and minimum values can be represented with 5-digit number? in 2's complement representation

What is the maximum and minimum values can be represented with 5-digit number that is assuming 2's complement representation? do I find the the min and maximum value of 5-digit numbers, which are 00000 and I'm not sure what the max is. Then convert…
me sung
  • 73
  • 1
  • 2
  • 8
2
votes
3 answers

Do we ignore overflow in Two's Complement

I'm trying to wrap my head around overflow within twos complement for example say I'm trying to take away these two binary numbers: 1111 1000 0100 - 010 111 001 000 I convert the 2nd binary number to it's two complement equivalent and then simply…
Wolf
  • 311
  • 1
  • 3
  • 12
2
votes
1 answer

Signed Hexadecimal value range

Say I had a 6 digit hexadecimal signed in two's complement. What would be its range? -(16 ^ 5) < x < (16 ^ 5) Correct?
Cheetah
  • 13,785
  • 31
  • 106
  • 190
2
votes
3 answers

2's complement binary multiplication

I am a little confuse with how do we perform signed 2's complement multiplication. 10 1101 -19 x 11 0001 x -15 ---------------- ------ 101101 285 000000 000000 000000 …
Gavin
  • 2,784
  • 6
  • 41
  • 78
2
votes
1 answer

How to convert from one's complement to two's complement

I am confused on how to do conversion from one's complement to two's complement. Suppose we have 10101101 in one's complement. How to convert it to two's complement? Thanks
UserMoon
  • 75
  • 1
  • 1
  • 4
2
votes
3 answers

Why both (byte) 400000 and (byte) -400000 result -128

In Java, why both (byte) 400000 and (byte) -400000 have result -128? Actually, I followed the calculation method from https://stackoverflow.com/a/9085666/1037217 For case: 400000 Binary: 1100001101010000000 Trim to 8 digits: 10000000 Since the left…
Felix
  • 31
  • 4
2
votes
5 answers

C - converting to 2s complement

I've decided to do it this way flip numbers 0=1, 1=0 add 1 to LSB if carry, loop until array[i]==0 But I'm stuck on the last point; how can I say that in a conditional loop?
tom
  • 41
  • 2
  • 2
  • 4
2
votes
1 answer

How to store a two's complement number in a long

I am converting a VB6 application to VB.net. The application uses an existing C library I can't change. The Problem: I am expecting a value around -180 or 180. The times that I expect 180, it works. But when I expect -180, I get the value…
JuiCe
  • 4,132
  • 16
  • 68
  • 119