twos-complement

Why does -INT_MIN = INT_MIN in a signed, two's complement representation?

血红的双手。 提交于 2019-11-26 20:58:28
I still haven't found a reason why the lowest signed negative number doesn't have an equivalent signed positive number? I mean in a 3 digit binary number for simplicity 100 is -4? but we can't have a positive 4 in signed format because we can't. It overflows. So how do we know two's complement 1000 is -4 1000 0000 is -128 and so on? We have no original positive number One way to think about it is that signed, two's complement format works by assigning each bit a power of two, then flipping the sign of the last power of two. Let's look at -4, for example, which is represented as 100. This means

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

▼魔方 西西 提交于 2019-11-26 16:48:13
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 definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two's complement signed versions equivalent? So, for example, 1111 in two's complement is equal to -1. If you add 1 to it and pretend that it's an unsigned number , you get 0000, which is correct even when thinking

carry/overflow & subtraction in x86

耗尽温柔 提交于 2019-11-26 16:09:45
问题 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 + 0001 = 1000 (7 + 1 = -8) pos+neg = pos (carry) 0011 + 1110 = 0001 (3 + -2 = 1) neg+neg = neg (carry) 1111 + 1111 = 1110 (-1 + -1 = -2) neg+neg = pos (overflow & carry) 1000 + 1001 = 0001 (-8 + -7 = 1) So, in x86 assembly, does subracting B from A

Why are signed and unsigned multiplication different instructions on x86(-64)?

不羁的心 提交于 2019-11-26 15:27:41
I thought the whole point of 2's complement was that operations could be implemented the same way for signed and unsigned numbers. Wikipedia even specifically lists multiply as one of the operations that benefits . So why does x86 have separate instructions for each, mul and imul ? Is this still true for x86-64? Addition and subtraction are the same, as is the low-half of a multiply. A full multiply, however, is not. Simple example: In 32-bit twos-complement, -1 has the same representation as the unsigned quantity 2**32 - 1. However: -1 * -1 = +1 (2**32 - 1) * (2**32 - 1) = (2**64 - 2**33 + 1)

Representation of negative numbers in C?

纵饮孤独 提交于 2019-11-26 15:25:30
How does C represent negative integers? Is it by two's complement representation or by using the MSB (most significant bit)? -1 in hexadecimal is ffffffff . So please clarify this for me. ISO C ( C99 section 6.2.6.2/2 in this case but it carries forward to later iterations of the standard (a) ) states that an implementation must choose one of three different representations for integral data types, two's complement, ones' complement or sign/magnitude (although it's incredibly likely that the two's complement implementations far outweigh the others). In all those representations, positive

Using -1 as a flag value for unsigned (size_t) types

元气小坏坏 提交于 2019-11-26 13:49:05
I was using -1 as a flag value for a function whose return type is size_t (an unsigned type). I didn't notice it at first, particularly because it wasn't causing any errors in my code (I was checking it with x == -1, not x < 0). Are there any subtle reasons I shouldn't leave it as is? When might this behave unexpectedly? Is this commonly used? ptrdiff_t is less common, takes longer to type, and anyway it's not really the appropriate type since the function returns an index into an array. -1 will always convert to the max unsigned value, this is due to section 4.7 Integral conversions : If the

Two&#39;s Complement in Python

对着背影说爱祢 提交于 2019-11-26 12:16:21
Is there a built in function in python which will convert a binary string, for example '111111111111', to the two's complement integer -1? travc Two's complement subtracts off (1<<bits) if the highest bit is 1. Taking 8 bits for example, this gives a range of 127 to -128. A function for two's complement of an int... def twos_comp(val, bits): """compute the 2's complement of int value val""" if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255 val = val - (1 << bits) # compute negative value return val # return positive value as is Going from a binary string is

Why are signed and unsigned multiplication different instructions on x86(-64)?

ぃ、小莉子 提交于 2019-11-26 08:52:33
问题 I thought the whole point of 2\'s complement was that operations could be implemented the same way for signed and unsigned numbers. Wikipedia even specifically lists multiply as one of the operations that benefits. So why does x86 have separate instructions for each, mul and imul ? Is this still true for x86-64? 回答1: Addition and subtraction are the same, as is the low-half of a multiply. A full multiply, however, is not. Simple example: In 32-bit twos-complement, -1 has the same

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

为君一笑 提交于 2019-11-26 06:45:56
问题 System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); is true. I understand that integer in Java is 32 bit and can\'t go above 2 31 -1, but I can\'t understand why adding 1 to its MAX_VALUE results in MIN_VALUE and not in some kind of exception. Not mentioning something like transparent conversion to a bigger type, like Ruby does. Is this behavior specified somewhere? Can I rely on it? 回答1: Because the integer overflows. When it overflows, the next value is Integer.MIN_VALUE .

Which arithmetic operations are the same on unsigned and two&#39;s complement signed numbers?

末鹿安然 提交于 2019-11-26 04:56:31
问题 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 definitive answer to the following question: Which of the arithmetic operators need signed versions, and for which are the unsigned and two\'s complement signed versions equivalent? So, for example, 1111 in two\'s complement is equal to -1. If