bit-shift

Why does right shifting negative numbers in C bring 1 on the left-most bits? [duplicate]

江枫思渺然 提交于 2019-11-27 18:45:22
问题 This question already has answers here : Are the shift operators (<<, >>) arithmetic or logical in C? (11 answers) Closed 4 years ago . The book "C The Complete Reference" by Herbert Schildt says that "(In the case of a signed, negative integer, a right shift will cause a 1 to be brought in so that the sign bit is preserved.)" What's the point of preserving the sign bit? Moreover, I think that the book is referring to the case when negative numbers are represented using a sign bit and not

Need help understanding “getbits()” method in Chapter 2 of K&R C

核能气质少年 提交于 2019-11-27 18:10:30
In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works. Here's the method provided: unsigned int getbits(unsigned int x, int p, int n) { return (x >> (p + 1 - n)) & ~(~0 << n); } The idea is that, for the given number x , it will return the n bits starting at position p , counting from the right (with the farthest right bit being position 0). Given the following main() method: int main(void) { int x = 0xF994, p = 4, n = 3; int z = getbits(x, p, n); printf("getbits(%u (%x), %d, %d) = %u (%X)\n", x, x, p, n, z, z);

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

孤人 提交于 2019-11-27 16:10:00
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 tests. (Maybe some of them are only working on 32bit systems) Live Code + JS+PHP results demo (click Run)

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 15:42:30
This question already has an answer here: Difference between >>> and >> 7 answers 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? The way negative numbers are represented is called 2's complement. To demonstrate how this works, take -12 as an example. 12, in

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

依然范特西╮ 提交于 2019-11-27 15:27:42
1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the bitwise shift? BitArray is going to be able to handle an arbitrary number of boolean values, whereas a byte will hold only 8, int only 32, etc. This is going to be the biggest difference between the two. Also, BitArray implements IEnumerable , where a integral type obviously does not. So it all depends on the requirements of your project; if you need an

Weird result of Java Integer left shift

大兔子大兔子 提交于 2019-11-27 15:13:18
I'm a little confused now by java left shift operation, 1<<31 = 0x80000000 --> this I can understand But 1<<32 = 1 Why is this? 1<<33 = 2 Looks like more shifting values, modulus 32 of the value is taken. Thanks everybody for the replying and giving the quote from JLS. I just want to know more. Any idea of the reason why it's designed in this way? Or is it just some convention? Apparently C doesn't have this quirk? Thanks to @paxdiablo. Looks like C declares this behaviour undefined. I have some personal assumption here: ARM architecture Reference Manual A7.1.38 Syntax LSL Rd, Rm, #immed_5

Should I bit-shift to divide by 2 in Java? [duplicate]

佐手、 提交于 2019-11-27 14:26:16
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is generally significantly faster. I'm not sure how Java has come along in that regards since the 9-10 years ago I learned about that. Does the Java compiler automatically converts a divide-by

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

血红的双手。 提交于 2019-11-27 12:22:37
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.. old_timer 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, which usually takes one clock cycle on modern processors (modern being an intentionally vague

Why use the Bitwise-Shift operator for values in a C enum definition?

扶醉桌前 提交于 2019-11-27 11:28:15
Apple sometimes uses the Bitwise-Shift operator in their enum definitions. For example, in the CGDirectDisplay.h file which is part of Core Graphics: enum { kCGDisplayBeginConfigurationFlag = (1 << 0), kCGDisplayMovedFlag = (1 << 1), kCGDisplaySetMainFlag = (1 << 2), kCGDisplaySetModeFlag = (1 << 3), kCGDisplayAddFlag = (1 << 4), kCGDisplayRemoveFlag = (1 << 5), kCGDisplayEnabledFlag = (1 << 8), kCGDisplayDisabledFlag = (1 << 9), kCGDisplayMirrorFlag = (1 << 10), kCGDisplayUnMirrorFlag = (1 << 11), kCGDisplayDesktopShapeChangedFlag = (1 << 12) }; typedef uint32_t CGDisplayChangeSummaryFlags;

Have you ever had to use bit shifting in real projects?

非 Y 不嫁゛ 提交于 2019-11-27 10:21:09
Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to use them? Nils Pipenbrinck I still write code for systems that do not have floating point support in hardware. In these systems you need bit-shifting for nearly all your arithmetic. Also you need shifts to generate hashes. Polynomial arithmetic (CRC, Reed-Solomon Codes are the mainstream applications) or uses shifts as well. However, shifts are just used because they are handy and express exactly what the writer intended.