bit-manipulation

Why is (-1 >>> 32) = -1? [duplicate]

微笑、不失礼 提交于 2019-12-17 19:22:17
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: why is 1>>32 == 1? -1 as an int converted to binary is represented by 32 1's. When I right-shift it 31 times, I get 1 (31 0's and one 1). But when I right-shift it 32 times, I get -1 again. Shouldn't it be equal to 0? 回答1: The Java specification explains the shift operators as follows: If the promoted type of the left-hand operand is int , only the five lowest-order bits of the right-hand operand are used as the

Using bitwise operators in C++ to change 4 chars to int

試著忘記壹切 提交于 2019-12-17 19:17:15
问题 What I must do is open a file in binary mode that contains stored data that is intended to be interpreted as integers. I have seen other examples such as Stackoverflow-Reading “integer” size bytes from a char* array. but I want to try taking a different approach (I may just be stubborn, or stupid :/). I first created a simple binary file in a hex editor that reads as follows. 00 00 00 47 00 00 00 17 00 00 00 41 This (should) equal 71, 23, and 65 if the 12 bytes were divided into 3 integers.

Emulating variable bit-shift using only constant shifts?

喜欢而已 提交于 2019-12-17 18:54:34
问题 I'm trying to find a way to perform an indirect shift-left/right operation without actually using the variable shift op or any branches. The particular PowerPC processor I'm working on has the quirk that a shift-by-constant-immediate, like int ShiftByConstant( int x ) { return x << 3 ; } is fast, single-op, and superscalar, whereas a shift-by-variable, like int ShiftByVar( int x, int y ) { return x << y ; } is a microcoded operation that takes 7-11 cycles to execute while the entire rest of

How to convert an int to a little endian byte array?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:34:12
问题 I have this function in C# to convert a little endian byte array to an integer number: int LE2INT(byte[] data) { return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; } Now I want to convert it back to little endian.. Something like byte[] INT2LE(int data) { // ... } Any idea? Thanks. 回答1: Just reverse it, Note that this this code (like the other) works only on a little Endian machine. (edit - that was wrong, since this code returns LE by definition) byte[] INT2LE(int data) {

Checking whether a number is positive or negative using bitwise operators

心不动则不痛 提交于 2019-12-17 18:28:09
问题 I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc. Can the same be done using bitwise operators and some trick in C or in C++? 回答1: Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc. Of course: bool is_positive = number > 0; bool is_negative = number < 0; bool is_zero = number =

Fast 24-bit array -> 32-bit array conversion?

喜欢而已 提交于 2019-12-17 18:20:04
问题 Quick Summary: I have an array of 24-bit values. Any suggestion on how to quickly expand the individual 24-bit array elements into 32-bit elements? Details: I'm processing incoming video frames in realtime using Pixel Shaders in DirectX 10. A stumbling block is that my frames are coming in from the capture hardware with 24-bit pixels (either as YUV or RGB images), but DX10 takes 32-bit pixel textures. So, I have to expand the 24-bit values to 32-bits before I can load them into the GPU. I

What is the purpose of the unsigned right shift operator “>>>” in Java?

我只是一个虾纸丫 提交于 2019-12-17 17:33:30
问题 I understand what the unsigned right shift operator ">>>" in Java does, but why do we need it, and why do we not need a corresponding unsigned left shift operator? 回答1: The >>> operator lets you treat int and long as 32- and 64-bit unsigned integral types, which are missing from the Java language. This is useful when you shift something that does not represent a numeric value. For example, you could represent a black and white bit map image using 32-bit int s, where each int encodes 32 pixels

Why is “i & (i ^ (i - 1))” equivalent to “i & (-i)”

馋奶兔 提交于 2019-12-17 17:10:14
问题 I had this in part of the code. Could anyone explain how i & (i ^ (i - 1)) could be reduced to i & (-i) ? 回答1: i ^ (i - 1) makes all bits after the last 1 bit of i becomes 1. For example if i has a binary representation as abc...de10...0 then i - 1 will be abc...de01...1 in binary. The part before the last 1 bit is not changed when subtracting 1 from i, so xor ing with each other returns 0 in that part, while the remaining will be 1 because of the difference in i and i - 1 . After that i & (i

Multiply two overflowing integers modulo a third

醉酒当歌 提交于 2019-12-17 17:00:36
问题 Given three integers, a , b and c with a,b <= c < INT_MAX I need to compute (a * b) % c but a * b can overflow if the values are too large, which gives the wrong result. Is there a way to compute this directly through bithacks, i.e. without using a type that won't overflow for the values in question? 回答1: Karatsuba's algorithm is not really needed here. It is enough to split your operands just once. Let's say, for simplicity's sake, that your numbers are 64-bit unsigned integers. Let k=2^32.

Bitwise operations equivalent of greater than operator

百般思念 提交于 2019-12-17 15:55:09
问题 I am working on a function that will essentially see which of two ints is larger. The parameters that are passed are 2 32-bit ints. The trick is the only operators allowed are ! ~ | & << >> ^ (no casting, other data types besides signed int, *, /, -, etc..). My idea so far is to ^ the two binaries together to see all the positions of the 1 values that they don't share. What I want to do is then take that value and isolate the 1 farthest to the left. Then see of which of them has that value in