bit-shift

Bitwise shift operators on signed types

↘锁芯ラ 提交于 2019-11-26 21:38:04
问题 I am trying to understand the behavior of bitwise operators on signed and unsigned types. As per the ISO/IEC document, following are my understandings. Left shift Operator The result of E1 << E2 , is E1 left-shifted E2 bit positions The vacated bits on the account of left shift will be filled by zeros. E1 as signed non-negative: E1 << E2 will result to E1 multiplied by 2 power of E2, if the value is representable by the result type. Q1: What about signed negatives? Q2: I am not able to

Is there any way to do 128-bit shifts on gcc <4.4?

爱⌒轻易说出口 提交于 2019-11-26 21:37:43
问题 gcc 4.4 seems to be the first version when they added int128_t . I need to use bit shifting and I have run out of room for some bit fields. Edit : It might be because I'm on a 32-bit computer, there's no way to have it for a 32-bit computer (Intel Atom), is there? I wouldn't care if it generated tricky slow machine code if I would work as expected with bit shifting. 回答1: I'm pretty sure that __int128_t is available on earlier versions of gcc. Just checked on 4.2.1 and FreeBSD and sizeof(_

warning: left shift count >= width of type

孤者浪人 提交于 2019-11-26 20:05:03
问题 I'm very new to dealing with bits and have got stuck on the following warning when compiling: 7: warning: left shift count >= width of type My line 7 looks like this unsigned long int x = 1 << 32; This would make sense if the size of long on my system was 32 bits. However, sizeof(long) returns 8 and CHAR_BIT is defined as 8 suggesting that long should be 8x8 = 64 bits long. What am I missing here? Are sizeof and CHAR_BIT inaccurate or have I misunderstood something fundamental? 回答1: long may

Java “Bit Shifting” Tutorial? [closed]

送分小仙女□ 提交于 2019-11-26 18:40:38
I would be thankful for a good tutorial, that explain for java newbies how in java all the "bit shifting" work. I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with byteshifting/bitmanipulation in java. This is just an example what I mean, (but I am looking for a tutorial that explains every possible operation): byte b = (byte)(l >> (8 - i << 3)); Andrzej Doyle Well, the official Java tutorial Bitwise and Bit Shift Operators covers the actual operations that are available in Java, and how to invoke them. If you

Difference between >>> and >> operators [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 18:33:37
问题 This question already has answers here : Difference between >>> and >> (7 answers) Closed 2 years ago . If the shifted number is positive >>> and >> work the same. If the shifted number is negative >>> fills the most significant bits with 1s whereas >> operation shifts filling the MSBs with 0. Is my understanding correct? If the negative numbers are stored with the MSB set to 1 and not the 2s complement way that Java uses the the operators would behave entirely differently, correct? 回答1: The

Is bit shifting O(1) or O(n)?

雨燕双飞 提交于 2019-11-26 18:10:44
问题 Are shift operations O(1) or O(n) ? Does it make sense that computers generally require more operations to shift 31 places instead of shifting 1 place? Or does it make sense the number of operations required for shifting is constant regardless of how many places we need to shift? PS: wondering if hardware is an appropriate tag.. 回答1: Some instruction sets are limited to one bit shift per instruction. And some instruction sets allow you to specify any number of bits to shift in one instruction

bit shifting with unsigned long type produces wrong results

随声附和 提交于 2019-11-26 17:51:52
I'm a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824 . Yet when I do 1 << 31 , I get the result of 2 ^ 64 (I think; actually this shouldn't be possible) which prints 18446744071562067968 . Can anyone explain this behaviour to me? 1 << 63 will be computed in int arithmetic, and your int is probably 32 bit. Remedy this by

Left bit shifting 255 (as a byte)

自作多情 提交于 2019-11-26 17:48:01
问题 Can anyone explain why the following doesn't compile? byte b = 255 << 1 The error: Constant value '510' cannot be converted to a 'byte' I'm expecting the following in binary: 1111 1110 The type conversion has stumped me. 回答1: Numeric literals in C# are int , not byte (and the bit shift will be evaluated by the compiler, hence only the 510 remains). You are therefore trying to assign a value to a byte which does not fit. You can mask with 255: byte b = (255 << 1) & 0xFF to reduce the result to

Java: right shift on negative number

陌路散爱 提交于 2019-11-26 17:34:57
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)? Alvin Wong Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>> . http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Arithmetic shift >> will keep

Unsigned Right Shift / Zero-fill Right Shift / >>> in PHP (Java/JavaScript equivalent)

血红的双手。 提交于 2019-11-26 17:25:41
问题 Before flagging this as a duplicate, please read below, and check my code * my updated code! So my problem is that, I have to implement Java/JavaScript '>>>' (Unsigned Right Shift / Zero-fill Right Shift), but I can't get it work exactly the same way. I've selected the 11 most promising implementations I've found on SO and on the web (links are added as comments in the code) and added a few test cases. Unfortunately NONE of the functions returned the same response as Java/JS to ALL of the