bit-manipulation

php array bitwise

大城市里の小女人 提交于 2019-12-07 01:47:17
问题 if I have an array of flags and I want to combine them with a bitwise conjunction ie: $foo = array(flag1, flag2); into $bar = flag1 | flag2; Does PHP have any good functions that will do this nicely for me already? 回答1: The array_reduce will reduce an array to a single value for you: $res = array_reduce($array, function($a, $b) { return $a | $b; }, 0); Reduce is also sometimes called fold (fold left or fold right) in other languages. 回答2: You could do it like so $bar = $foo[0] | $foo[1] If

Removing bit at specific index

喜欢而已 提交于 2019-12-07 01:13:54
问题 I'm basically trying to remove a bit from an integer at a specific index. That is, I do not want to unset/clear the bit; I actually want to strip it, so that every higher bit moves down, replacing the respective bit at its position. Visually, this could be compared to deleting an element from an array or removing a character from a string. For the sake of clarity, some examples: 1011011 (original number) ^ index = 2 0101111 (result) 10000000000000000000000000000001 ^ index = 31

Bitwise saturated addition in C (HW)

我的未来我决定 提交于 2019-12-07 01:02:43
问题 I'm working on an assignment and I can't figure out how to implement this. I have to make a function sadd(int x, int y) that returns the numbers added together unless it overflows (then just return the max possible int). I've been able to come up with some solutions involving casting and conditional statements, but those aren't allowed in the solution. Only the operators ~ ! ^ + << >> & and | . 回答1: For addition of signed numbers, overflow has happened if you add two numbers of the same sign

Bit parity code for odd number of bits

不羁岁月 提交于 2019-12-07 00:28:10
问题 I am trying to find the parity of a bitstring so that it returns 1 if x has an odd # of 0's. I can only use basic bitwise operations and what I have so far passes most of the tests, but I'm wondering 2 things: Why does x ^ (x + ~1) work? I stumbled upon this, but it seems to give you 1 if there are an odd number of bits and something else if even. Like 7^6 = 1 because 7 = 0b0111 Is this the right direction of problem solving for this? I'm assuming my problem is stemming from the first

Any smarter way to extract from array of bits?

天涯浪子 提交于 2019-12-06 22:40:24
问题 I have areas of memory that could be considered "array of bits". They are equivalent to unsigned char arr[256]; But it would be better thought of as bit arr[2048]; I'm accessing separate bits from it with #define GETBIT(x,in) ((in)[ ((x)/8) ] & 1<<(7-((x)%8))) but I do it a lot in many places of the code, often in performance-critical sections and I wonder if there are any smarter, more optimal methods to do it. extra info: Architecture: ARM9 (32 bit); gcc/Linux. The physical data

Fast average without division

无人久伴 提交于 2019-12-06 21:51:48
问题 I have a binary search loop which gets hit many times in the execution path. A profiler shows that the division part of the search (finding the middle index given the high and low indices of the search range) is actually the most costly part of the search, by a factor of about 4. (I think) it is not critical for efficient binary search to find the exact middle value, just a value near the middle which does not have bias in either direction. Is there a bit-twiddling algorithm to replace mid =

Is it possible to check if any of 2 sets of 3 ints is equal with less than 9 comparisons?

放肆的年华 提交于 2019-12-06 20:16:14
问题 int eq3(int a, int b, int c, int d, int e, int f){ return a == d || a == e || a == f || b == d || b == e || b == f || c == d || c == e || c == f; } This function receives 6 ints and returns true if any of the 3 first ints is equal to any of the 3 last ints. Is there any bitwise-hack similar way to make it faster? 回答1: Expanding on dawg's SSE comparison method, you can combine the results of the comparisons using a vector OR, and move a mask of the compare results back to an integer to test

set most significant bit in C

走远了吗. 提交于 2019-12-06 18:46:48
问题 I am trying to set the most significant bit in a long long unsigned, x. To do that I am using this line of code: x |= 1<<((sizeof(x)*8)-1); I thought this should work, because sizeof gives size in bytes, so I multiplied by 8 and subtract one to set the final bit. Whenever I do that, the compiler has this warning: "warning: left shift count >= width of type" I don't understand why this error is occurring. 回答1: The 1 that you are shifting is a constant of type int , which means that you are

Converting to Binary using bitwise and bitshift

∥☆過路亽.° 提交于 2019-12-06 16:49:19
I am trying to create a function to print a number in binary using bitwise and bit shifting but I am having trouble printing it correctly. The following is my code. void PrintInBinary( unsigned int decNum ) { int i = 0; unsigned int highestOne = 1 << (sizeof(unsigned int)*8 - 1); for( i = 0; i < sizeof(int)*8; i++ ) { printf( "%u", decNum & (highestOne >> i) ); } printf("\n"); } int main() { unsigned int a = 128; PrintInBinary( a ); system("PAUSE"); return 0; } The following is the output: 0000000000000000000000001280000000 Basically, its printing the 2^bit rather than just a 1 at each bit

Check if flag is set in integer variable

若如初见. 提交于 2019-12-06 16:33:34
I am making my own simple drawing engine. I am trying to determine if a variable has been set to a specific value using what I think is called bitwise comparison but I maybe wrong. I've always been a bit confused about what the following is and how I use it: int DRAW_REPEAT_X = 70001; // I have a feeling I should make this value binary instead of a unique number, ie, 0 int DRAW_REPEAT_Y = 70002; // I have a feeling I should make this value binary instead of a unique number, ie, 2 int drawMethod = DRAW_REPEAT_X | DRAW_REPEAT_Y; // this means I want to repeat an image on both the x and y axis