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

Can someone explain overflow in C# using binary?

I'm currently reading a book on C# programming and it has brief intro to Overflow and Underflow and the writer gives a general idea of what happens when you go over the allowed range of a certain type. Example short a = 30000; short b =…
Shabubble
  • 73
  • 5
7
votes
2 answers

How does Java's bit shift operator work under the hood?

I didn't study IT, and only very recently came across bit shifts and an application for two's complement. So, can you please use simple English in your explanations and assume I know hardly anything about IP addresses, bit operations, and Java…
Christian
  • 6,070
  • 11
  • 53
  • 103
7
votes
1 answer

Two's complement sign extension python?

I'm wondering if there's a way to do a two's complement sign extension as you would in C/C++ in Python, using standard libraries (preferably on a bitarray). C/C++: // Example program #include #include int main() { int x =…
Vasu
  • 1,090
  • 3
  • 18
  • 35
7
votes
3 answers

Which arithmetic operations are the same on unsigned and two's complement signed numbers?

I'm designing a simple toy instruction set and accompanying emulator, and I'm trying to figure out what instructions to support. In the way of arithmetic, I currently have unsigned add, subtract, multiply, and divide. However, I can't seem to find a…
joshlf
  • 21,822
  • 11
  • 69
  • 96
7
votes
4 answers

Why we need to add 1 while doing 2's complement

The 2's complement of a number which is represented by N bits is 2^N-number. For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001) 7==> 0111 1's compliment of…
user85
  • 1,526
  • 5
  • 26
  • 42
6
votes
1 answer

How to convert a negative integer into two's complement binary form? (Java)

I need to convert numbers, positive and negative, into binary format - so, 2 into "00000010", and -2 into "11111110", for example. I don't need more than 12 bits or so, so if the string is longer than that I can just trim off the leading sign bits.…
jbreed
  • 1,514
  • 5
  • 22
  • 35
6
votes
5 answers

Largest Java Short (32767) plus 1 not turning negative?

recently I learned about the two's compliment method of representing both positive and negative integers in the base two system. I then tried to see this in action using java with the following short code: int a=2147483647; System.out.println("a:…
Brown Philip
  • 219
  • 1
  • 3
  • 12
6
votes
1 answer

How does 1 left shift by 31 (1 << 31) work to get maximum int value? Here are my thoughts and some explanations I found online

I'm fairly new to bit manipulation and I'm trying to figure out how (1 << 31) - 1 works. First I know that 1 << 31 is 1000000000000000000000000000 and I know it's actually complement of minimum int value, but when I tried to figure out (1 << 31) -…
HM9527
  • 111
  • 1
  • 1
  • 5
6
votes
1 answer

Using 2's Complement to Perform Binary Division for Signed Number

First of all, this is not a duplicate question of this because it hasn't answered my questions below. I searched for many resources and ended with no clear perception of how to perform signed number division using 2's complement, specifically for…
user2963216
  • 391
  • 2
  • 4
  • 11
6
votes
2 answers

"Bitwise Not" in Python disconsidering 2's complement

I need to do a "~" operation in Python but not taking account of 2's complement. I managed to do that by using XOR, do you know another way to do this? (more efficient) a = 0b101 b = 0b10101 print bin(a ^ (2 ** a.bit_length() - 1)) #0b10 print…
Leandro Moreira
  • 377
  • 3
  • 16
6
votes
5 answers

How to prove that the C statement -x, ~x+1, and ~(x-1) yield the same results?

I want to know the logic behind this statement, the proof. The C expression -x, ~x+1, and ~(x-1) all yield the same results for any x. I can show this is true for specific examples. I think the way to prove this has something to do with the…
Ben Fossen
  • 1,331
  • 6
  • 21
  • 33
6
votes
3 answers

Read / Write Haskell Integer in two's complement representation

I need to read and write Integers in a way that is compatible with what Java does with it's BigInteger class: Returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order:…
Waldheinz
  • 10,399
  • 3
  • 31
  • 61
5
votes
4 answers

Why Long.toHexString(0xFFFFFFFF) returns ffffffffffffffff

This is what I see in java, and it puzzles me. Long.toHexString(0xFFFFFFFF) returns ffffffffffffffff Similarly, 0xFFFFFFFF and Long.parseLong("FFFFFFFF", 16) are unequal.
Vic
  • 21,473
  • 11
  • 76
  • 97
5
votes
2 answers

How does the hardware know whether a variable is positive or negative?

I'm sorry if this question is too basic...I just have not found the answer to it anywhere. Say I declare a C variable like this: unsigned int var = 241; In this case the var is unsigned so my intention is for it to have decimal value…
Cantaff0rd
  • 705
  • 1
  • 6
  • 14
5
votes
2 answers

sra(shift right arithmetic) vs srl (shift right logical)

Please take a look at these two pieces of pseudo-assembly code: 1) li $t0,53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 2) li $t0,-53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print…
user591931
  • 239
  • 2
  • 3
  • 10