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

Counting the binary digits of a signed 32-bit integer in c programming

I have following function which counts the number of binary digits in an unsigned 32-bit integer. uint32_t L(uint32_t in) { uint32_t rc = 0; while (in) { rc++; in >>= 1; } return(rc); } Could anyone tell me please in…
rzmuc
  • 243
  • 1
  • 5
  • 10
-3
votes
4 answers

Bitwise addition of opposite signs

int main(){ int a = 10, b = -2; printf("\n %d \n",a^b); return 0; } This program outputs -12. I could not understand how. Please explain. 0111 1110 -> 2's complement of -2 0000 1010 -> 10 --------- 0111 0100 This no seems…
Angus
  • 12,133
  • 29
  • 96
  • 151
-3
votes
2 answers

Number theory: solution need

Suppose a = a31 a30 . . . a1 a0 is a 32-bit binary word. Consider the 32-bit binary word b = b31 b30 . . . b1 b0 computed by the following algorithm: Scan a from right to left and copy its bits to b until the first 1 is found (which is also…
mystery
  • 875
  • 6
  • 16
-3
votes
1 answer

Why does -~x equal x+1?

In Obfuscated C Code Contest 2006. Please explain sykes2.c, there is a statement "-~i == i+1 because of twos-complement". Can someone explain why this is the case?
Thomas Song
  • 717
  • 4
  • 7
-4
votes
1 answer

Does CPU work only with two´s complement?

I have some doubts about how two's complement is implemented in CPU. Does the CPU always work with two's complement?. If not, how it knows when to apply it for substraction. From my point of view, I though that CPU operate with bits and that one´s…
Sosa
  • 3
  • 4
-4
votes
1 answer

How does sign contraction work from 16 bit to 8 bit?

How do I sign contract ff12.here it is a negative number but I need to remove all the FF here but if I do that it becomes a positive number. One definition of "sign contraction" can be found in this online copy of The Art of Assembly, as the…
-4
votes
1 answer

two's compliment, and binary numbers example

So let me get this straight, just started learning about two's compliment etc, anyway I got a binary number 10101011 which is 171 I was told to find the 8 bit one's and two's compliment of this number... I put 84 and 85 respectively... Is this…
Sim
  • 570
  • 1
  • 10
  • 22
-5
votes
1 answer

How are numbers represented in a computer and what is the role of floating-point and twos-complement?

I have very general question about how computers work with numbers. In general computer systems only know binary - 0 and 1. So in memory any number is a sequence of bits. It does not matter if the number represented is a int or float. But when does…
Skully
  • 347
  • 2
  • 4
  • 19
1 2 3
45
46