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
5
votes
4 answers

Why Do We have unsigned and signed int type in C?

I am a beginner in C . I have recently learned about 2's Complement and other ways to represent negative number and why 2's complement was the most appropriate one. What i want to ask is for example, int a = -3; unsigned int b = -3; //This is the…
5
votes
1 answer

Explain a line of two's complement math counting free virtual memory

I have a Visual Studio 2008 C++ application for Windows Mobile 6.x where I'm counting the amount of free virtual memory available for a given process. (I realize it is not taking fragmentation in to account.) My code looks basically like…
PaulH
  • 7,759
  • 8
  • 66
  • 143
5
votes
3 answers

Why does this two's complement shortcut work?

A shortcut method of forming the two's complement of a binary number is to copy bits from the right until a one-bit has been copied, then complement (invert) the remaining bits. That's explained on SO here and also on Wikipedia. What is not…
Bob Brown
  • 1,463
  • 1
  • 12
  • 25
5
votes
2 answers

Explain why x == ~(~x + 1) + 1 (two's complement and back!)

As we all know usually negative numbers in memory represents as two's complement numbers like that from x to ~x + 1 and to get back we don't do the obvious thing like ~([~x + 1] - 1) but instead we do ~[~x + 1] + 1 can someone explain why does it…
Solar Dia
  • 53
  • 4
5
votes
1 answer

Why can't a negative normalized floating point binary number start with 11?

Studying for A level computing we are repeatedly told that a negative normalised floating point binary number is not normalised if it starts with 11 by textbooks, exam questions and teachers. In the case of minus 11 in twos compliment it can be…
5
votes
1 answer

Java Bitwise Or Between Byte and Int

I am trying to perform a bitwise or on a byte value I have in Java. For example, I am running: byte b = (byte)0b11111111; int result = 0 | b; My expected result for this would be 0b00000000 00000000 00000000 11111111, or 255. However, I am…
5
votes
2 answers

Why does the java method Integer.toBinaryString(-128) output seven digits?

The simple scenario: You have a byte array byte[] message = { 1, 2, 3 }; To print it out in binary you can use the code: for (byte b : message) { System.out.println(Integer.toBinaryString(0x100 + b).substring(1)); } (Got that code from this…
snickers10m
  • 1,709
  • 12
  • 28
5
votes
1 answer

Minimum Number of Bits Required for Two's Complement Form

On my midterm, there was a question stating: Given the decimal values, what is the minimum number of bits required to represent each number in Two's Complement form? The values were: -26, -1, 10, -15, -4. I did not get this question right…
Andrew T
  • 783
  • 4
  • 11
  • 20
5
votes
2 answers

Two's Complement disadvantage?

I was reading about two's complement, I understand this method is most efficient, but there might be some disadvantages too. I could not find any disadvantages, Is there any situation where the conversion to two's complement could fail to represent…
user845405
  • 1,461
  • 5
  • 23
  • 43
5
votes
2 answers

Java two's complement binary to integer

I know that converting a decimal to binary with Integer.toBinaryString(355) = 0000000101100011 and Integer.toBinaryString(-355) = 1111111010011101 (where I take the lower 16 bits of the 32 bit…
omegaFlame
  • 245
  • 3
  • 9
  • 21
5
votes
2 answers

How to make the 2-complement of a number without using adder

In two-complement to invert the sign of a number you usually just negate every bit and add 1. For example: 011 (3) 100 + 1 = 101 (-3) In VHDL is: a <= std_logic_vector(unsigned(not(a)) + 1); In this way the synthesizer uses an N-bit adder. Is…
HBv6
  • 3,487
  • 4
  • 30
  • 43
5
votes
2 answers

Converting from 8 bit to 16 bit

I was wondering how do you convert from an 8 bit 2's complement to a 16 bit 2's complement signed number? 1100 0110 is an example
Masterminder
  • 1,127
  • 7
  • 21
  • 31
4
votes
1 answer

VHDL complement counter issues: converting std_logic to integer

Essentially, my question is: "can this not be done any easier?"; and what 'this' is, follows below (code too): I wanted to have a sort of a 'complement' counter function, implemented in VHDL, which would basically invert/complement/not the counter…
sdaau
  • 36,975
  • 46
  • 198
  • 278
4
votes
1 answer

Just started learning C this semester, but I can't seem to understand what the professor wants from us in this question

Original question: A positive number has the same representation in one’s complement and in two’s complement. Suppose its representation is interpreted as two’s complement, and its additive inverse is determined. Now this representation is …
cinos
  • 117
  • 2
  • 8
4
votes
1 answer

What is the relationship between 2’s complement and IEEE754?

I used to think that all numbers are stored in computers in 2’s complement format. And there is no -0 in 2’s complement. But in IEEE754, there are both +0 and -0. And I read that in JavaScript, all numbers are IEEE754. So now I am confused. What’s…
T.T
  • 321
  • 2
  • 10