bit-shift

Is a logical right shift by a power of 2 faster in AVR?

孤者浪人 提交于 2019-11-28 03:50:10
问题 I would like to know if performing a logical right shift is faster when shifting by a power of 2 For example, is myUnsigned >> 4 any faster than myUnsigned >> 3 I appreciate that everyone's first response will be to tell me that one shouldn't worry about tiny little things like this, it's using correct algorithms and collections to cut orders of magnitude that matters. I fully agree with you, but I am really trying to squeeze all I can out of an embedded chip (an ATMega328) - I just got a

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

喜夏-厌秋 提交于 2019-11-28 02:51:37
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? user unknown 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 beyond their bounds: 1111 1110 << 2 1111 1000 it is filled from the right with fresh zeros. :) 0001

C left shift on 64 bits fail

旧时模样 提交于 2019-11-28 01:57:12
I have this code in C (it's for study only): char x; uint64_t total = 0; for(x = 20; x < 30; x++){ total = (((((1 << x) * x) / 64) + 1) * sizeof(uint64_t)); printf("%d - %llu\n", x, total); } What is printed: 20 - 2621448 21 - 5505032 22 - 11534344 23 - 24117256 24 - 50331656 25 - 104857608 26 - 218103816 27 - 18446744073625665544 28 - 18446744073575333896 29 - 18446744073508225032 Why at x > 26 do I have those strange values? I'm at gcc 4.6.1 on Ubuntu 10.10 64 bits. Because 1 is an int , 32 bits, so (1 << 27)*27 overflows. Use 1ull . Regarding your comment, if x is a uint64_t , then 1 << x

Why does (1 << 31) >> 31 result in -1? [duplicate]

若如初见. 提交于 2019-11-28 01:28:32
This question already has an answer here: why shift int a=1 to left 31 bits then to right 31 bits, it becomes -1 4 answers int z = 1; z <<= 31; z >>= 31; printf ("%d\n",z); When I run the code, z=-1 , why? AlexD int z = 1; z <<= 31; Assuming int is 32 bit and two's complement representation is used, the left shift is undefined behavior in C because the result if not representable in the int type. From the standard: The result of E1 << E2 is E1 left-shifted E2 bit positions ... If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the

Bitshift in javascript

情到浓时终转凉″ 提交于 2019-11-28 00:40:39
I've got a really big number: 5799218898. And want to shift it right to 13 bits. So, windows-calculator or python gives me: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 70791 | 10001010010000111 As expected. But Javascript: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 183624 | 101100110101001000 I think it because of internal integer representation in javascript, but cannot find anything about that. In ECMAScript (Javascript) bitwise operations are always in 32-bit. Therefore 5799218898 is chopped into 32-bit which becomes 1504251602. This integer >> 13 gives 183624. In

Bitwise shift operators on signed types

守給你的承諾、 提交于 2019-11-28 00:06:28
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 understand on what's meant by "reduced modulo" in the following context. "If E1 has an unsigned type, the

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

北城余情 提交于 2019-11-27 23:59:34
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. janm I'm pretty sure that __int128_t is available on earlier versions of gcc. Just checked on 4.2.1 and FreeBSD and sizeof(__int128_t) gives 16. You could also use a library. This would have the advantage that it is portable

Shifting a 32 bit integer by 32 bits

好久不见. 提交于 2019-11-27 23:19:47
I'm slinging some C code and I need to bitshift a 32 bit int left 32 bits. When I run this code with the parameter n = 0, the shifting doesn't happen. int x = 0xFFFFFFFF; int y = x << (32 - n); Why doesn't this work? Shift at your own peril. Per the standard, what you want to do is undefined behavior . C99 §6.5.7 3 - The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined. In other words

Difference between SHL and SAL in 80x86

余生颓废 提交于 2019-11-27 23:00:40
问题 I have learned how to work with 80x86 assembler, so in bit-wise shift operation, I faced a problem with SAL and SHL usage. I means the difference between lines of code as follow : MOV X, 0AAH SAL X, 4 MOV X, 0AAH SHL X, 4 When we should use SHL and when use SAL? What is the difference of them? 回答1: According to this, they are the same: The shift arithmetic left (SAL) and shift logical left (SHL) instructions perform the same operation; they shift the bits in the destination operand to the

Right shift and signed integer

℡╲_俬逩灬. 提交于 2019-11-27 22:49:44
On my compiler, the following pseudo code (values replaced with binary): sint32 word = (10000000 00000000 00000000 00000000); word >>= 16; produces a word with a bitfield that looks like this: (11111111 11111111 10000000 00000000) My question is, can I rely on this behaviour for all platforms and C++ compilers? Andrew Clark From the following link: INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand Noncompliant Code Example (Right Shift) The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1