bitwise-operators

GMP Bit shift doesn't work on negative numbers

十年热恋 提交于 2019-12-11 01:57:20
问题 I found this function at php.net. It seems to work on positive numbers, but fails on negative ones: function gmp_shiftr($x,$n) { // shift right return(gmp_div($x,gmp_pow(2,$n))); } echo -1 >> 8; //returns -1, presumably correctly echo "<br />"; echo gmp_strval(gmp_shiftr(-1,8)); //returns 0, presumably incorrectly How could I fix up the function to work with negatives? Two ideas I have: Maybe I could do something along the lines of if (whatever) { $a >> $b} else{ gmp_shiftr($a, $b) }? Or,

OpenCV - Retaining only marked blobs in python

China☆狼群 提交于 2019-12-11 01:52:01
问题 I have a morphological problem i am attempting to solve using OpenCV. I have two images. Mask Seed In the mask image am trying to retain only the blobs marked by a seed image and to remove the rest. Underneath I am posting the mask and seed image Mask Image : Seed Image : To further illustrate a the problem I have zoomed into the image and created a subplot. In this example the plot on your right is the seed image, the plot your left is the mask image. At the end of the operation I would like

Why do I have to cast 0 (zero) when doing bitwise operations in C#?

元气小坏坏 提交于 2019-12-10 23:17:41
问题 I am using the following code to sign extend a 12 bit value I unpacked from 1.5 bytes to 16 bits: word[0] |= ((word[0] & 0x800) != 0 ? (Int16)(-4096) : (Int16)0); If I don't cast the last zero to Int16 I get the following complaints from the compiler: Warning 1 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first Error 2 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Why is this? I

Greater than function in C

可紊 提交于 2019-12-10 21:25:14
问题 I know this is an age old question and you probably have come across this aswell, but there's a bug in my solution and I don't know how to solve it. I need to write a function that compares two integers. I am only allowed to use the operations (!,~,&,^,|,+,>>,<<) and also no control structures(if,else loops etc). isGreater(int x, int y) { //returns 1 if x > y. return ((y+(~x+1))>>31)&1; } my idea is simple, we compute y-x, we shift by 31 to get the sign bit, if it's negative, then we return

Bitwise complement operator

谁说胖子不能爱 提交于 2019-12-10 20:33:29
问题 Can you guys please explain the below program int main() { int max = ~0; printf("%d\n",max); return 0; } AFAIK ~ will flip the bits. In this case i.e ~0 will set all the bits into 1. So max variable should contain MAX value but I am getting o/p as -1. So can anyone here please tell me why I am getting o/p as -1. 回答1: Why did you expect to obtain the "max value"? In 2's-complement signed representation all-1 bit pattern stands for -1 . It is just the way it is. Maximum value in 2's-complement

Cast some light on population count algorithm

橙三吉。 提交于 2019-12-10 19:19:35
问题 I looked for good methods to do popcount (count of set bits). I found this one, here http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan unsigned int v; // count the number of bits set in v unsigned int c; // c accumulates the total bits set in v for(c = 0; v; c++) { v &= v - 1; // clear the least significant bit set } Trying on a few examples, its true that it works. What property of binary operations / representation makes it work? Could you hint at some further

Bitwise right shift operator in Java

假如想象 提交于 2019-12-10 17:35:25
问题 In Java, -4 >> 2 gives -1 but -5 >> 2 gives -2. Can anybody explain why? Here is a sample code: byte r=-5; r>>=2; System.out.println(r); Also in this scenario >> and >>> operators give the same answer. Can anyone explain that as well? 回答1: You can take a look at the bits. Using two's complement notation, the bits for -4 and -5 are, showing only the last 8 bits for brevity: -4: 1111 1100 -5: 1111 1011 Bit shifting to the right 2 positions, with sign extension: -4 >> 2: 1111 1111 (-1) -5 >> 2:

C fastest way to compare two bitmaps

て烟熏妆下的殇ゞ 提交于 2019-12-10 17:29:51
问题 There are two arrays of bitmaps in the form of char arrays with millions of records. What could be fastest way to compare them using C. I can imagine to use bitwise operator xor 1 byte at a time in a for loop. Important point about bitmaps: 1% to 10% of times algorithm is run, bitmaps can differ. Most of the time they will be same. When hey can differ, they can as much as 100%. There is high probability of change of bits in continuous streak. Both bitmaps are of same length. Aim: Check do

How to interleave 2 booleans using bitwise operators?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 17:01:21
问题 Suppose I have two 4-bit values, ABCD and abcd . How to interleave it, so it becomes AaBbCcDd , using bitwise operators? Example in pseudo-C: nibble a = 0b1001; nibble b = 0b1100; char c = foo(a,b); print_bits(c); // output: 0b11010010 Note: 4 bits is just for illustration, I want to do this with two 32bit ints. 回答1: This is called the perfect shuffle operation, and it's discussed at length in the Bible Of Bit Bashing, Hacker's Delight by Henry Warren, section 7-2 "Shuffling Bits." Assuming x

How to use bit operations in GLSL 1.3 with OpenGL 2.1

回眸只為那壹抹淺笑 提交于 2019-12-10 16:09:15
问题 I'm trying to write a shader that uses many bit operations. In fact they are supported since glsl 1.30, but I'm only on OpenGL 2.1. Is there any way to use bit operations with my OpenGL version? 回答1: All SM3 compatible (~OpenGL 2.1) hardware supports limited integer functionality. This is usually done by emulating integers with floats and does not include bit operations. For bit operations, you need either GLSL 1.3 or EXT_gpu_shader4. If the reason that you have only OpenGL 2.1 is that your