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

Which 2's complement integer operations can be used without zeroing high bits in the inputs, if only the low part of the result is wanted?

In assembly programming, it's fairly common to want to compute something from the low bits of a register that isn't guaranteed to have the other bits zeroed. In higher level languages like C, you'd simply cast your inputs to the small size and let…
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
14
votes
2 answers

carry/overflow & subtraction in x86

I'm trying to wrap my head around overflow & carry flags in x86. As I understand it, for addition of signed 2's complement numbers, the flags can only be generated in one of four ways (my examples are 4-bit numbers): pos+pos = neg (overflow) 0111…
Robz
  • 1,747
  • 5
  • 21
  • 35
14
votes
3 answers

2's complement hex number to decimal in java

I have a hex string that represents a 2's complement number. Is there an easy way (libraries/functions) to translate the hex into a decimal without working directly with its bits?? E.G. This is the expected output given the hex on the left: "0000"…
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
14
votes
2 answers

Convert a signed decimal to hex encoded with two's complement

I need to encode a signed integer as hexadecimal using via the two's complement notation. For example I would like to convert e.g. -24375 to 0xffffa0c9. So far I have been working on the following lines: parseInt(-24375).toString(2) >…
diffa
  • 2,986
  • 1
  • 22
  • 35
14
votes
1 answer

How to create a 32-bit integer from eight (8) 4-bit integers?

Let's say I have a max 32-bit integer - const a = ((2 ** 32) - 1) const b = parseInt("11111111111111111111111111111111", 2) // 32 bits, each is a one! console.log(a === b) // true console.log(a.toString(2)) //…
Mulan
  • 129,518
  • 31
  • 228
  • 259
13
votes
7 answers

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

I am reading the book: CS-APPe2. C has unsigned and signed int type and in most architectures uses two's-complement arithmetic to implement signed value; but after learning some assembly code, I found that very few instructions distinguish between…
tomwang1013
  • 1,349
  • 2
  • 13
  • 27
13
votes
5 answers

How does C know what type to expect?

If all values are nothing more than one or more bytes, and no byte can contain metadata, how does the system keep track of what sort of number a byte represents? Looking into Two's Complement and Single Point on Wikipedia reveals how these numbers…
Jack Stout
  • 1,265
  • 3
  • 12
  • 25
12
votes
1 answer

How does the NEG instruction affect the flags on x86?

The Intel Software Development Manual says this about the neg instruction: The CF flag set to 0 if the source operand is 0; otherwise it is set to 1. The OF, SF, ZF, AF, and PF flags are set according to the result. I thought that AF and CF would…
tbodt
  • 16,609
  • 6
  • 58
  • 83
12
votes
6 answers

Convert 2 bytes to a number

I have a control that has a byte array in it. Every now and then there are two bytes that tell me some info about number of future items in the array. So as an example I could have: ... ... Item [4] = 7 Item [5] = 0 ... ... The value of this is…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
12
votes
6 answers

two's complement of numbers in python

I am writing code that will have negative and positive numbers all 16 bits long with the MSB being the sign aka two's complement. This means the smallest number I can have is -32768 which is 1000 0000 0000 0000 in two's complement form. The largest…
user3299406
  • 141
  • 1
  • 1
  • 5
11
votes
1 answer

What is ^0 in golang?

I am seeing ^0 in the code base. Example: type stat struct { ... min int64 ... } newStat := stat{min: ^0} What does ^0 mean?
samol
  • 18,950
  • 32
  • 88
  • 127
11
votes
4 answers

How is an integer stored in memory?

This is most probably the dumbest question anyone would ask, but regardless I hope I will find a clear answer for this. My question is - How is an integer stored in computer memory? In c# an integer is of size 32 bit. MSDN says we can store numbers…
Mike
  • 3,204
  • 8
  • 47
  • 74
10
votes
6 answers

How to print a signed integer as hexadecimal number in two's complement with python?

I have a negative integer (4 bytes) of which I would like to have the hexadecimal form of its two's complement representation. >>> i = int("-312367") >>> "{0}".format(i) '-312367' >>> "{0:x}".format(i) '-4c42f' But I would like to see "FF..."
none
  • 103
  • 1
  • 1
  • 4
10
votes
1 answer

Java binary literals - Value -128 for byte

Since SE 7 Java allows to specify values as binary literal. The documentation tells me 'byte' is a type that can hold 8 Bit of information, the values -128 to 127. Now i dont know why but i cannot define 8 bits but only 7 if i try to assign a binary…
JBA
  • 2,769
  • 5
  • 24
  • 40
10
votes
10 answers

2's complement example, why not carry?

I'm watching some great lectures from David Malan (here) that is going over binary. He talked about signed/unsigned, 1's compliment, and 2's complement representations. There was an addition done of 4 + (-3) which lined up like this: 0100 1101 (flip…
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
1 2
3
45 46