twos-complement

Unsigned / Signed Arithmetic Problems from A Programmer's Perspective Textbook

随声附和 提交于 2019-12-09 01:57:37
问题 int x = random(); int y = random(); unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it always yields 1, describe the underlying mathematical principles. Otherwise, give an example of arguments that make it yield 0. A. (x<y) == (-x>-y) B. ((x+y)<<4) + y-x == 17*y+15*x C. ~x+~y+1 == ~(x+y) D. (ux-uy) == -(unsigned)(y-x) E. ((x >> 2) << 2) <= x For these questions, I got that

Why does the negation of the minimum possible integer yield itself?

亡梦爱人 提交于 2019-12-08 11:00:02
问题 So I wrote a little experiment testing underflowing & overflowing, using c and a 64 bit machine. For type int the min/max values are: int tmax = 2147483647; int tmin = -2147483648; I know understand how two's-complement works, that isn't my question. So I thought, well what happens if I make something negative tmin? That is: int tmin = -2147483648; int negativeTmin = -tmin; It ends up still being tmin. (That is, negativeTmin will be -2147483648) My question is why is that? Since positive 2

14-bit left-justified two's complement to a signed short

霸气de小男生 提交于 2019-12-07 17:07:39
问题 I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?) What would be the fastest way to do that? 回答1: Simply divide by 4. (Note, right-shift leads to implementation/undefined behaviour.) 回答2: A portable solution: short convert(unsigned char hi, unsigned char lo) { int s = (hi << 6) | (lo >> 2); if (s >= 8192) s -= 16384; return s; } 来源: https://stackoverflow.com/questions/14710764/14

How to get 2's complement of a binary number in Java programmatically

可紊 提交于 2019-12-06 11:15:32
问题 How to calculate the 2's Complement of a Hex number in Android/Java. For Example : String x = 10011010; 1's complement of x = 01100101; 2's complement is 01100110; How I can pro-grammatically achieve in Java? I had tried the following code to convert the binary to its 1's compliment: public String complementFunction(String bin) { String ones = ""; for (int i = 0; i < bin.length(); i++) { ones += flip(bin.charAt(i)); } return ones; } // Returns '0' for '1' and '1' for '0' public char flip(char

14-bit left-justified two's complement to a signed short

风格不统一 提交于 2019-12-05 21:39:42
I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?) What would be the fastest way to do that? Oliver Charlesworth Simply divide by 4. (Note, right-shift leads to implementation/undefined behaviour.) A portable solution: short convert(unsigned char hi, unsigned char lo) { int s = (hi << 6) | (lo >> 2); if (s >= 8192) s -= 16384; return s; } 来源: https://stackoverflow.com/questions/14710764/14-bit-left-justified-twos-complement-to-a-signed-short

Why does this two's complement shortcut work?

一个人想着一个人 提交于 2019-12-05 19:11:57
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 explained is why this shortcut works, that is, why does it produce the same result as inverting all the bits and adding one. So, my question is, why does this work? It works because adding one to a binary number is accomplished by flipping all 1s to 0s from the right until a 0 is reached, flip that to 1 and stop (essentially carrying the overflow of adding

-128 and 128 in 2's complement

↘锁芯ラ 提交于 2019-12-05 16:43:12
问题 In 2's complement, 0-127 is represented as 00000000 to 01111111. In case of negative numbers, we invert all bits in the unsigned representation and add 1 to get the 2's complement. (Reference: http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement) so -1 in 2's complement will be: unsigned 1 = 00000001 invert all bits = 11111110 add 1 = 11111111 But for -128, if we follow the same steps: unsigned 128 = 10000000 invert all bits= 01111111 add 1= 10000000 so -128 and 128

String.format() and hex numbers in Java

走远了吗. 提交于 2019-12-05 08:20:43
问题 I'm trying to figure out why String.format() is behaving the way it does. Context: Systems programming class, writing an assembler. There is a 5 character hex field in the object file, which I am creating from a value. Tried using: String.format("%05X", decInt); This works as intended for positive numbers (11 -> 0000B ) However it fails for negative numbers (-1 -> FFFFFFFF instead of FFFFF ) I suppose I could just take a substring of the last 5 characters, but I would still like to figure out

Two's complement binary form

偶尔善良 提交于 2019-12-04 20:49:50
In a TC++ compiler, the binary representation of 5 is (00000000000000101) . I know that negative numbers are stored as 2's complement, thus -5 in binary is (111111111111011) . The most significant bit (sign bit) is 1 which tells that it is a negative number. So how does the compiler know that it is -5 ? If we interpret the binary value given above (111111111111011) as an unsigned number, it will turn out completely different? Also, why is the 1's compliment of 5 -6 (1111111111111010) ? The compiler doesn't know . If you cast -5 to unsigned int you'll get 32763 . The compiler knows because this

Detect one's or two's complement architecture in C++?

心不动则不痛 提交于 2019-12-04 19:18:11
问题 What is the most reliable way to detect whether the architecture uses one's or two's complement representation in C++? 回答1: You shouldn't have to worry - there aren't too many ones complement machines out there :) But the easiest thing might be to compare "-1" with ~0. 来源: https://stackoverflow.com/questions/16501091/detect-ones-or-twos-complement-architecture-in-c