twos-complement

How does 1 left shift by 31 (1 << 31) work to get maximum int value? Here are my thoughts and some explanations I found online

大憨熊 提交于 2019-12-12 22:53:11
问题 I'm fairly new to bit manipulation and I'm trying to figure out how (1 << 31) - 1 works. First I know that 1 << 31 is 1000000000000000000000000000 and I know it's actually complement of minimum int value, but when I tried to figure out (1 << 31) - 1, I found an explanation states that, it's just 10000000000000000000000000000000 - 1 = 01111111111111111111111111111111 I was almost tempted to believe it since it's really straightforward. But is this what really happening? If it's not, why it

What is good way to negate an integer in binary operation in python?

那年仲夏 提交于 2019-12-12 18:04:15
问题 Based on what I've read about the binary representation of integers, the first bit is for sign (positive or negative). Let's say we have an integer x = 5 and sys.getsizeof(x) returns 28 (that is binary representation in 28 bit). For now I am trying to flip the first bit to 1 by using x|=(1<<27) but it returns 134217733 . I was just wondering whether it needs to be some negative number? (not -5) Is there anything wrong with what I am doing? 回答1: You can't switch a Python int from positive to

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

两盒软妹~` 提交于 2019-12-12 08:55:42
问题 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 always work? I think I can proof it with 1-bit, 2-bit, 3-bit numbers and then use Mathematical induction but it doesn't help me understand how exactly that works. Thanks! 回答1: That's the same thing anyway. That is, ~x + 1 == ~(x - 1) . But let's put

Signed Hexadecimal string to long int function

佐手、 提交于 2019-12-12 04:13:09
问题 I need a function to convert a 32bit or 24bit signed (in two's complement) hexadecimal string into a long int. Needs to work on both 32bit and 64bit machines (regardless of the size of long int) and work regardless of whether the machine is a two's complement machine or not. SOLUTION: long int hex2li (char hexStr[], int signedHex) { int bits = strlen (hexStr) * 4; char *pEnd; long long int result = strtoll (hexStr, &pEnd, 16); if (pEnd[0] == '\0') { if (signedHex) { if (result >= (1LL <<

Doubleword-length 2’s complement representation of Binary number?

雨燕双飞 提交于 2019-12-11 17:13:44
问题 Doing my first assignment in Assembler and this is the question posed... 1.Find the doubleword-length 2’s complement representation of each of the following decimal numbers. (Steps of getting your results must be displayed, otherwise you get zero point.) a -100 b -55555 I have a very foreign professor that I completely can't understand so I'm in need of some assistance. So a doubleword would be 32 bits which would make... A) 0000|0000|0000|0000|0000|0001|0000|0000 ( 1 at beginning for

How come in two's complementary, 1001 and 11111001 are both -7?

风流意气都作罢 提交于 2019-12-11 06:08:50
问题 When I learned two's complimentary, I was taught, that for a signed number, 0111 represents 7 , so by using two's complementary, 0111 -> 1000 + 1 -> 1001, is -7 so 1001 represents -7 . While I refreshed this concept on YouTube, I see a video that is saying, 0000 0111 represents 7 , so by using two's complementary, 0000 0111 -> 1111 1000 + 1 -> 1111 1001, is -7, thus, 11111001 represents -7 . I got confused. So by just looking at a signed binary number, how can we determine its value? I

Two's Complement of a number and its negative number difference?

只谈情不闲聊 提交于 2019-12-11 02:59:12
问题 So i was told that two's complement is usually to used to find complement of a number and I used it only to complement positive numbers (i.e positve --> negative conversion) however I just got an example in book which asks me the following : Express 23, -23, and -9 in 8-bit binary two’s Complement form now what does that mean? 23 means -23 in binary and -23 means 23 ? sorta confused over there 回答1: 2's complement is used to represent negative numbers, which in turn, can be used to do

Why is 1's complement still used for encoding vector instructions?

瘦欲@ 提交于 2019-12-10 17:53:37
问题 In an answer, jww points out that 1's complement is still used in encoding vector instructions on intel architectures, and Ruslan clarifies that these instructions are being used more as auto-vectorization becomes common. Is there an advantage of 1's complement that causes it to continue to be used in these instructions, or is it simply being used for historical reasons? Quoting jww: From Intel® 64 and IA-32 Architectures Software Developer’s Manual 2A, page 3-8: 3.1.1.8 Description Section

Why does this two's complement shortcut work?

◇◆丶佛笑我妖孽 提交于 2019-12-10 07:01:07
问题 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? 回答1: It works because adding one to a binary number is accomplished by flipping all 1s to

Two's complement conversion

谁说我不能喝 提交于 2019-12-10 01:54:50
问题 I need to convert bytes in two's complement format to positive integer bytes. The range -128 to 127 mapped to 0 to 255. Examples: -128 (10000000) -> 0 , 127 (01111111) -> 255, etc. EDIT To clear up the confusion, the input byte is (of course) an unsigned integer in the range 0 to 255. BUT it represents a signed integer in the range -128 to 127 using two's complement format. For example, the input byte value of 128 (binary 10000000) actually represents -128. EXTRA EDIT Alrighty, lets say we