bit-shift

Undefined behavior of right-shift in C++

一笑奈何 提交于 2019-11-30 11:32:24
From cppreference.com: For unsigned a and for signed a with nonnegative values, the value of a >> b is the integer part of a/2 b . For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative). In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined. Why do we have an undefined behavior in case the right operand is greater or equal to the number of bits in the promoted left operand? It seems

bit shift operation does not return expected result

余生长醉 提交于 2019-11-30 09:48:50
Why does Java return -2147483648 when I bit shift 1 << 63 ? The expected result is 9 223 372 036 854 775 808 , tested with Wolfram Alpha and my calculator. I tested: System.out.print((long)(1 << (63))); Adam Mihalcin There's an important thing to note about the line System.out.print((long)(1 << (63))); You first take (1 << 63) , and then you cast to long. As a result, you are actually left-shifting in integers, so the long cast doesn't have any effect. That's why shifting 63 bits left gives the min integer rather than the min long. But there's another, more important point. Java longs are

circular left shift of an array by n positions in java

为君一笑 提交于 2019-11-30 09:43:38
I am trying to do the circular left shift of an array by n positions using only a single 1D array. I can do it in two arrays, but I haven't figured out how to do it using one. Please give your suggestions There is actually a clever algorithm for that. We'll use A to denote the array, N to denote the array size, and n to denote the number of positions to shift. After the shift you would like the i-th element to move to the ((i + n) mode N)-th position, Hence we can define the new positions by the following mapping: f(j) := (j + n) mod N (j = 0,...,N - 1) The general idea behind this algorithm

Bit shifting an int 32 times in C

喜你入骨 提交于 2019-11-30 08:50:20
问题 I have a specific C bit-shifting scenario that I do not believe is covered on Stack Overflow yet. (If it is, I haven't been able to find it!) This exercise is using signed int s as the data type. Take the value 0x80000000 (which is just a 1 in the most significant bit.) Shift it right once on a machine using arithmetic right-shifts (which mine does). Result = 0xC0000000 (1100 0000 in leftmost byte). Continue shifting it and you should be filling up with ones, from the left to the right.

Shifting a Java BitSet

我怕爱的太早我们不能终老 提交于 2019-11-30 08:23:07
I am using a java.util.BitSet to store a dense vector of bits. I want to implement an operation that shifts the bits right by 1, analogous to >>> on ints. Is there a library function that shifts BitSet s? If not, is there a better way than the below? public static void logicalRightShift(BitSet bs) { for (int i = 0; (i = bs.nextSetBit(i)) >= 0;) { // i is the first bit in a run of set bits. // Set any bit to the left of the run. if (i != 0) { bs.set(i - 1); } // Now i is the index of the bit after the end of the run. i = bs.nextClearBit(i); // nextClearBit never returns -1. // Clear the last

Why use only the lower five bits of the shift operand when shifting a 32-bit value? (e.g. (UInt32)1 << 33 == 2)

帅比萌擦擦* 提交于 2019-11-30 08:00:20
问题 Consider the following code: UInt32 val = 1; UInt32 shift31 = val << 31; // shift31 == 0x80000000 UInt32 shift32 = val << 32; // shift32 == 0x00000001 UInt32 shift33 = val << 33; // shift33 == 0x00000002 UInt32 shift33a = (UInt32)((UInt64)val << 33); // shift33a == 0x00000000 It doesn't generate a warning (about using a shift greater than 32) so it must be an expected behavior. The code that actually gets put out to the generated assembly (or at least Reflector's interpretation of the code)

Shift Operators in C++

你离开我真会死。 提交于 2019-11-30 07:11:09
If the value after the shift operator is greater than the number of bits in the left-hand operand, the result is undefined. If the left-hand operand is unsigned, the right shift is a logical shift so the upper bits will be filled with zeros. If the left-hand operand is signed, the right shift may or may not be a logical shift (that is, the behavior is undefined). Can somebody explain me what the above lines mean?? I'm assuming you know what it means by shifting. Lets say you're dealing with a 8-bit char s unsigned char c; c >> 9; c >> 4; signed char c; c >> 4; The first shift, the compiler is

Bit Twiddling Hacks: interleave bits the obvious way [closed]

橙三吉。 提交于 2019-11-30 04:57:11
i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html ) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; // bits of x are in the even positions and y in the odd; unsigned int z = 0; // z gets the resulting Morton Number. for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) // unroll for more speed... { z |= (x & 1U << i) << i | (y & 1U << i) << (i + 1); } can someone explain to me how this works with an example? for example if we have x = 100101 and y = 010101 , what will be result?

What does AND 0xFF do?

若如初见. 提交于 2019-11-29 20:17:07
In the following code: short = ((byte2 << 8) | (byte1 & 0xFF)) What is the purpose of &0xFF ? Because other somestimes I see it written as: short = ((byte2 << 8) | byte1) And that seems to work fine too? John Colanduoni Anding an integer with 0xFF leaves only the least significant byte. For example, to get the first byte in a short s , you can write s & 0xFF . This is typically referred to as "masking". If byte1 is either a single byte type (like uint8_t ) or is already less than 256 (and as a result is all zeroes except for the least significant byte) there is no need to mask out the higher

Is unsigned long int correct for this operation?

末鹿安然 提交于 2019-11-29 18:09:38
Here's my code: #include <stdio.h> int main(int argc, char *argv[]) { unsigned long int x = 0; // trying to make x = 2,147,483,648 x = 1 << 31; printf("%lu", x); } It's returning that x = 18446744071562067968. I read that unsigned long int should go up to 4,294,967,296, so why can't I use 1 << 32 to set x equal to 2,147,483,648? 1 << 31 causes undefined behaviour, if your system has 32-bit ints. The literal 1 is a signed int. You need to do an unsigned shift instead of a signed shift: x = 1UL << 31; I added L so that the code is still correct even on a 16-bit system, and it doesn't hurt to do