bit-shift

Where can I find a bit shifting guide for C? [closed]

别等时光非礼了梦想. 提交于 2019-12-03 10:01:27
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I have looked at What are bitwise shift (bit-shift) operators and how do they work? but I still find the concept of bit shifting difficult to understand. Can someone point me in the direction of a more basic guide to bit shifting in C. I expect it will be something really long

Java results differ for (int)Math.pow(2,x) and 1<<x

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:05:28
Why do the following two operations yield different results in Java for x = 31 or 32 but the same results for x=3 ? int x=3; int b = (int) Math.pow(2,x); int c = 1<<x; Results: x=32: b=2147483647; c=1; x=31: b=2147483647; c=-2147483648; x=3: b=8 ; c=8 There are multiple issues at play: An int can only store values between -2147483648 and 2147483647 . 1 << x only uses the lowest five bits of x . Thus, 1 << 32 is by definition the same as 1 << 0 . Shift operations are performed on the two's-complement integer representation of the value of the left operand ; this explains why 1 << 31 is negative

Will bit-shift by zero bits work correctly?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:20:44
Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int . I'm particularly concerned about call with bitCount equal to zero - will it work correctly then? Also is there a chance that a compiler seeing the whole code of the function when compiling its call site will reduce calls with bitCount equal to zero to a no-op? It is certain that at least one C++ compiler will recognize the situation (when the 0 is known at compile time) and

What does a >> mean in Go language?

為{幸葍}努か 提交于 2019-12-03 03:15:21
I’m looking for information on Google’s Go language. In “A Tour of Go” they have this code: const ( Big = 1<<100 Small = Big>>99 ) But what do << and >> mean? You can see all of the code at http://tour.golang.org/#14 Jon Purdy They are bitwise shift operators . x << y means x × 2 y , while x >> y means x × 2 −y or, equivalently, x ÷ 2 y . These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by a power of two has the effect of “shifting” the digits left or right, respectively: // Left shift

Bitshifting a long in Java

孤者浪人 提交于 2019-12-03 02:00:41
Im sure this is an easy one for whoever sees it first! Why in Java does code like long one = 1 << 0; long thirty = 1 << 30; long thirtyOne = 1 << 31; long thirtyTwo = 1 << 32; System.out.println(one+" = "+Long.toBinaryString(1 << 0)); System.out.println(thirty+" = "+Long.toBinaryString(1 << 30)); System.out.println(thirtyOne+" = "+Long.toBinaryString(1 << 31)); System.out.println(thirtyTwo+" = "+Long.toBinaryString(1 << 32)); print 1 = 1 1073741824 = 1000000000000000000000000000000 -2147483648 = 1111111111111111111111111111111110000000000000000000000000000000 1 = 1 This does not make sense to

How can I turn an int into three bytes in Java?

心已入冬 提交于 2019-12-03 00:50:04
I am trying to convert an int into three bytes representing that int (big endian). I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it. For example: int myInt; // some code byte b1, b2 , b3; // b1 is most significant, then b2 then b3. *Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing. To get the least significant byte: b3 = myInt & 0xFF; The 2nd least significant byte: b2 = (myInt >> 8) & 0xFF; And the 3rd least significant byte: b1 = (myInt >> 16) & 0xFF; Explanation: Bitwise ANDing a

What is the >>>= operator in C?

穿精又带淫゛_ 提交于 2019-12-03 00:01:58
问题 Given by a colleague as a puzzle, I cannot figure out how this C program actually compiles and runs. What is this >>>= operator and the strange 1P1 literal? I have tested in Clang and GCC. There are no warnings and the output is "???" #include <stdio.h> int main() { int a[2]={ 10, 1 }; while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) printf("?"); return 0; } 回答1: The line: while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) contains the digraphs :> and <: , which translate to ] and [ respectively, so it

How does this print “hello world”?

有些话、适合烂在心里 提交于 2019-12-02 13:48:29
I discovered this oddity: for (long l = 4946144450195624l; l > 0; l >>= 5) System.out.print((char) (((l & 31 | 64) % 95) + 32)); Output: hello world How does this work? The number 4946144450195624 fits 64 bits, its binary representation is: 10001100100100111110111111110111101100011000010101000 The program decodes a character for every 5-bits group, from right to left 00100|01100|10010|01111|10111|11111|01111|01100|01100|00101|01000 d | l | r | o | w | | o | l | l | e | h 5-bit codification For 5 bits, it is posible to represent 2⁵ = 32 characters. English alphabet contains 26 letters, this

What is the >>>= operator in C?

帅比萌擦擦* 提交于 2019-12-02 13:46:40
Given by a colleague as a puzzle, I cannot figure out how this C program actually compiles and runs. What is this >>>= operator and the strange 1P1 literal? I have tested in Clang and GCC. There are no warnings and the output is "???" #include <stdio.h> int main() { int a[2]={ 10, 1 }; while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) printf("?"); return 0; } Ilmari Karonen The line: while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] ) contains the digraphs :> and <: , which translate to ] and [ respectively, so it's equivalent to: while( a[ 0xFULL?'\0':-1 ] >>= a[ !!0X.1P1 ] ) The literal 0xFULL is the

Why does 3,758,096,384 << 1 give 768

天涯浪子 提交于 2019-12-02 10:11:17
问题 After reading the great answer for Absolute Beginner's Guide to Bit Shifting? I tested the claim (sic): 3,758,096,384 << 1 from Chrome console: 3,758,096,384 << 1 > 768 3,758,096,384 << 2 > 1536 3758096384 << 1 > -1073741824 回答1: That's the comma operator at work. It's actually 384 << 1 . (The comma operator evaluates its left hand side, then evaluates its right hand side, and returns the right hand side.) 回答2: It returns 768 because you're incorrectly using the comma operator. 3,758,096,384