bit-shift

How do shift operators work in Java? [duplicate]

心不动则不痛 提交于 2019-11-26 11:34:33
This question already has an answer here: What are bitwise shift (bit-shift) operators and how do they work? 9 answers I am trying to understand the shift operators and couldn't get much. When I tried to execute the below code System.out.println(Integer.toBinaryString(2 << 11)); System.out.println(Integer.toBinaryString(2 << 22)); System.out.println(Integer.toBinaryString(2 << 33)); System.out.println(Integer.toBinaryString(2 << 44)); System.out.println(Integer.toBinaryString(2 << 55)); I get the below 1000000000000 100000000000000000000000 100 10000000000000 1000000000000000000000000 Could

Is multiplication and division using shift operators in C actually faster?

左心房为你撑大大i 提交于 2019-11-26 11:32:43
Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any sort of input that can't be multiplied or divided in this way? Short answer: Not likely. Long answer: Your compiler has an optimizer in it that knows how to multiply as quickly as your target processor architecture is capable. Your best bet is to tell the compiler your intent clearly (i.e. i*2 rather than i << 1) and let it decide what the fastest

What does >> do in java?

落花浮王杯 提交于 2019-11-26 11:07:17
问题 Okay, I tried looking up what >>, or shift means, but it\'s way over my head as this site explains it: http://www.janeg.ca/scjp/oper/shift.html So can someone explain it like they\'re talking to a kid? 回答1: Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s. Bitshifting is simply moving those sequences of 1s and 0s left or right. So all the >> operator does is shift the bits towards the right one bit. Consider the number 101: // Assuming signed 8

unsigned right Shift &#39;>>>&#39; Operator in Java [duplicate]

这一生的挚爱 提交于 2019-11-26 10:58:33
问题 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? 回答1: 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

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

风流意气都作罢 提交于 2019-11-26 09:24:17
问题 What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ? 回答1: 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.) 回答2: Another alternative: if (BigInteger.valueOf(value).testBit(x)) { // ... } 回答3: 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

Is left and right shifting negative integers defined behavior?

萝らか妹 提交于 2019-11-26 09:10:31
问题 I know, right shifting a negative signed type depends on the implementation, but what if I perform a left shift? For example: int i = -1; i << 1; Is this well-defined? I think the standard doesn\'t say about negative value with signed type if E1 has a signed type and non-negative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. It only clarifies that if the result isn\'t representable in signed type then the

c get nth byte of integer

心已入冬 提交于 2019-11-26 09:07:53
问题 I know you can get the first byte by using int x = number & ((1<<8)-1); or int x = number & 0xFF; But I don\'t know how to get the nth byte of an integer. For example, 1234 is 00000000 00000000 00000100 11010010 as 32bit integer How can I get all of those bytes? first one would be 210, second would be 4 and the last two would be 0. 回答1: int x = (number >> (8*n)) & 0xff; where n is 0 for the first byte, 1 for the second byte, etc. 回答2: For the (n+1)th byte in whatever order they appear in

Why does a shift by 0 truncate the decimal?

两盒软妹~` 提交于 2019-11-26 06:39:56
问题 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

Does Go compiler&#39;s evaluation differ for constant expression and other expression

我的梦境 提交于 2019-11-26 06:08:01
问题 Why does below code fail to compile? package main import ( \"fmt\" \"unsafe\" ) var x int = 1 const ( ONE int = 1 MIN_INT int = ONE << (unsafe.Sizeof(x)*8 - 1) ) func main() { fmt.Println(MIN_INT) } I get an error main.go:12: constant 2147483648 overflows int Above statement is correct. Yes, 2147483648 overflows int (In 32 bit architecture). But the shift operation should result in a negative value ie -2147483648. But the same code works, If I change the constants into variables and I get the

Java: right shift on negative number

南笙酒味 提交于 2019-11-26 06:06:38
问题 I am very confused on right shift operation on negative number, here is the code. int n = -15; System.out.println(Integer.toBinaryString(n)); int mask = n >> 31; System.out.println(Integer.toBinaryString(mask)); And the result is: 11111111111111111111111111110001 11111111111111111111111111111111 Why right shifting a negative number by 31 not 1 (the sign bit)? 回答1: Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>> .