bit-shift

When to use Shift operators << >> in C#?

社会主义新天地 提交于 2019-11-27 04:23:48
问题 I was studying shift operators in C#, trying to find out when to use them in my code. I found an answer but for Java, you could: a) Make faster integer multiplication and division operations: *4839534 * 4* can be done like this: 4839534 << 2 or 543894 / 2 can be done like this: 543894 >> 1 Shift operations much more faster than multiplication for most of processors. b) Reassembling byte streams to int values c) For accelerating operations with graphics since Red, Green and Blue colors coded

Why does a shift by 0 truncate the decimal?

守給你的承諾、 提交于 2019-11-27 04:14:46
I recently found this piece of JavaScript code: Math.random() * 0x1000000 << 0 I understood that the first part was just generating a random number between 0 and 0x1000000 (== 16777216). But the second part seemed odd. What's the point of performing a bit-shift by 0? I didn't think that it would do anything. Upon further investigation, however, I noticed that the shift by 0 seemed to truncate the decimal part of the number. Furthermore, it didn't matter if it was a right shift, or a left shift, or even an unsigned right shift. > 10.12345 << 0 10 > 10.12345 >> 0 10 > 10.12345 >>> 0 10 I tested

Why was 1 << 31 changed to be implementation-defined in C++14?

不想你离开。 提交于 2019-11-27 04:12:34
In all versions of C and C++ prior to 2014, writing 1 << (CHAR_BIT * sizeof(int) - 1) caused undefined behaviour, because left-shifting is defined as being equivalent to successive multiplication by 2 , and this shift causes signed integer overflow: The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. [...] If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined . However in C++14 the text has changed for << but not for multiplication: The

unsigned right Shift '>>>' Operator in Java [duplicate]

落爺英雄遲暮 提交于 2019-11-27 03:56:45
Possible Duplicate: Why is (-1 >>> 32) = -1? The unsigned right shift operator inserts a 0 in the leftmost. So when I do System.out.println(Integer.toBinaryString(-1>>>30)) output 11 Hence, it is inserting 0 in the left most bit. System.out.println(Integer.toBinaryString(-1>>>32)) output 11111111111111111111111111111111 Shouldn't it be 0? See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19 If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were

What's the reason high-level languages like C#/Java mask the bit shift count operand?

穿精又带淫゛_ 提交于 2019-11-27 02:44:33
问题 This is more of a language design rather than a programming question. The following is an excerpt from JLS 15.19 Shift Operators: If the promoted type of the left-hand operand is int , only the five lowest-order bits of the right-hand operand are used as the shift distance. If the promoted type of the left-hand operand is long , then only the six lowest-order bits of the right-hand operand are used as the shift distance. This behavior is also specified in C#, and while I'm not sure if it's in

Shift Operators in C++

和自甴很熟 提交于 2019-11-27 02:26:28
问题 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?? 回答1: I'm assuming you know what it means by shifting. Lets say you're dealing

Bitwise operations and shifts

无人久伴 提交于 2019-11-27 02:18:28
问题 Im having some trouble understanding how and why this code works the way it does. My partner in this assignment finished this part and I cant get ahold of him to find out how and why this works. I've tried a few different things to understand it, but any help would be much appreciated. This code is using 2's complement and a 32-bit representation. /* * fitsBits - return 1 if x can be represented as an * n-bit, two's complement integer. * 1 <= n <= 32 * Examples: fitsBits(5,3) = 0, fitsBits(-4

Please explain the difference between logical shift right, arithmatic shift right, and rotate right

南笙酒味 提交于 2019-11-27 01:58:45
问题 I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt seems too simple. 回答1: First remember that machine words are of fixed size. Say 4, and that your input is: +---+---+---+---+ | a | b | c | d | +---+---+---+---+ Then pushing everything one position to the left gives: +---+---+---+---+ | b | c | d | X | +---+---+---+---+ Question what to put as X?

Java: Checking if a bit is 0 or 1 in a long

拈花ヽ惹草 提交于 2019-11-27 00:34:00
What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ? I'd use: if ((value & (1L << x)) != 0) { // The bit was set } (You may be able to get away with fewer brackets, but I never remember the precedence of bitwise operations.) Another alternative: if (BigInteger.valueOf(value).testBit(x)) { // ... } Ande I wonder if: if (((value >>> x) & 1) != 0) { } .. is better because it doesn't matter whether value is long or not, or if its worse because it's less obvious. Tom Hawtin - tackline Jul 7 at 14:16 You can also use bool isSet = ((value>>x) & 1) != 0; EDIT: the

What does a bitwise shift (left or right) do and what is it used for?

狂风中的少年 提交于 2019-11-26 23:50:34
问题 I've seen the operators >> and << in various code that i've looked at (none of which I actually understood), but I'm just wondering what they actually do and what some practical uses of them are. EDIT If the shifts are like x * 2 and x / 2 , what is the real difference from actually using the * and / operators? Is there a performance difference? 回答1: Here is an applet where you can exercise some bit-operations, including shifting. You have a collection of bits, and you move some of them