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

How to make python look hex numbers as two's complemented?

I have an array of hex positive and negetive numbers. I want to transform them to decimal value: >>> int("f107",16) 61703 >>> how can I make python to look f107 as a two's complemented number? In the other word I want -3833 instead of 61703. How…
Ebrahim Ghasemi
  • 5,850
  • 10
  • 52
  • 113
3
votes
1 answer

Converting an int into a 32-bit binary line with extra stuff

A user inputs a string of 8 characters that's then converted to string and put into an array to be played with. With those 8 digits I'd like to be able to convert them into 32-bit binary e.g 0000 0000 0000 0000 0000 0000 0000 0000 int =…
Jube
  • 184
  • 2
  • 15
3
votes
2 answers

Check if a number can be represented using n bits in 2’s complement

I'm working on a function that returns 1 when x can be represented as an n-bit, 2’s complement number and 0 if it can't. Right now my code works for some examples like (5, 3), (-4, 3). But I can't get it to work for instances where n is bigger than…
Rbutler93
  • 73
  • 1
  • 11
3
votes
1 answer

Decimal <--> Two's Complement <--> Hex conversion

I'm wondering if I get a question like: "Convert a decimal number to two's complement, then give your answer in Hex". Is the path below, how one would do it? Decimal number: -23 23 = 00010111 = in hex 17 = -17 -23 = 11101001 = in hex E9 So to…
DangerousDave23
  • 113
  • 1
  • 2
  • 10
3
votes
2 answers

Converting two bytes into 16 bit signed number

I got two bytes: byte[0] is 0000 0000 byte[1] is 1000 0000 I would like to put them together and make one float value of them. So the result should be 128 is decimal and 0000 0000 1000 0000 in binary. Not negative because of the leading zero. My…
Gizmo
  • 871
  • 1
  • 15
  • 38
3
votes
5 answers

n bit 2s binary to decimal in C++

I am trying to convert a string of signed binary numbers to decimal value in C++ using stoi as shown below. stoi( binaryString, nullptr, 2 ); My inputs are binary string in 2s format and stoi will work fine as long as the number of digits is…
kona
  • 143
  • 1
  • 3
  • 8
3
votes
2 answers

14-bit left-justified two's complement to a signed short

I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?) What would be the fastest way to do that?
Maestro
  • 9,046
  • 15
  • 83
  • 116
3
votes
3 answers

Binary Subtraction with 2's Complement

I need help subtracting with binary using 2's representation and using 5 bits for each number: 1) -9 -7 = ? Is there overflow? -9 = 01001 (2's complement = 10111) and -7 = 00111 (2's complement = 11001) Now we need to add because we're using 2's…
iOSDev
  • 1,028
  • 4
  • 13
  • 21
3
votes
4 answers

Explain Use of ":" operator in c++ in the code snippet "int i:2;"

Possible Duplicate: What does this C++ code mean? In the following C++ code # include int main() { struct clap { int i:2; int j:2; int k:3; }x={1,2,3}; printf("%d %d %d",x.i,x.j,x.k); return 0; } On running the…
amol_beast
  • 281
  • 1
  • 2
  • 8
3
votes
1 answer

2's(N) = 1's(N-1)

There is an interesting fact: The 2's complement of a number N is equivalent to 1's complement of the number N minus 1. i.e. 2's(N) = 1's(N-1) The below result is obvious. 2's(N) = 1's(N) + 1 How the first result can be proved with the help…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
3
votes
2 answers

negative floating-point numbers represented like negative integer numbers?

I know how computer today stores negative integers, which most of the computers use the 2' complement. I just wast wondering the 2' complement method applies for all kinds of numbers like floating points as well?
ipkiss
  • 13,311
  • 33
  • 88
  • 123
2
votes
3 answers

Why do integers in Java integer not use all the 32 or 64 bits?

I was looking into 32-bit and 64-bit. I noticed that the range of integer values that can stored in 32 bits is ±4,294,967,295 but the Java int is also 32-bit (If I am not mistaken) and it stores values up to ±2 147 483 648. Same thing for long, it…
Jeel Shah
  • 3,274
  • 17
  • 47
  • 68
2
votes
2 answers

How to add and subtract 16 bit floating point half precision numbers?

How do I add and subtract 16 bit floating point half precision numbers? Say I need to add or subtract: 1 10000 0000000000 1 01111 1111100000 2’s complement form.
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
2
votes
4 answers

How do you differentiate -1 from 0xff when reading a byte from a Java FileInputStream?

So, I had to take a test for a job interview where I was required to write a mini app that performed simple XOR encryption, and came across this question. I used a FileInputReader to pull each byte in, perform an XOR operation with the key, and…
Ron Brown
  • 21
  • 1
  • 2
2
votes
2 answers

How Can I Convert Hex 'F933F177' to Decimal -114.036361 via SnowSQL or a Java function?

I have coordinates stored in HEX, which from searching online appear to have used Signed 2's Complement to handle the negative values. I'm getting a bit lost with where the various conversions are made, I think the path should be: Convert hex to…