bit-manipulation

Bit manipulation- for negative numbers

我是研究僧i 提交于 2019-12-11 03:05:46
问题 Let the size of integer i=-5 be 2 bytes. The signed bit value at the leftmost bit is '1'(which signifies that it is a negative number). When i am trying to do a right shift operation, should i not expect the '1' at the 15th bit position to shift to 14th position? and give me a high but positive value? What i tried: int i=5; i>>1 // giving me 2 (i understand this) int i=-5 i>>1 // giving me -3 (mind=blown) 回答1: Right shifts of negative values are implementation-defined, [expr.shift]/3 The

Java On AND'ing a short with an short, it is upgraded to int and returns weird values

馋奶兔 提交于 2019-12-11 02:16:13
问题 I'm creating a mask and setting higher bits in a short like this: enum FLAGS {FLAG1, FLAG2, FLAG3, FLAG4, FLAG5, FLAG6}; public static void setFlag(short len, FLAGS flag) { short mask = 1 << (Short.SIZE - flag.ordinal() - 1); len |= mask; } I printed the values: len: 0000001111111100 mask : 1000000000000000 after OR'ing with mask: 11111111111111111000001111111100 I understand that when we do bit manipulation on shorts they're upgrded to int to avoid overflow, but why are all the higher bits

How to find F(x,0) when F(x,i) = F(x-1,i) xor F(x-1, i+1) xor … F(x-1,n) in less than linear time

馋奶兔 提交于 2019-12-11 02:07:59
问题 Given a base array, I need to compute the value of a function given below: A[] = { a0, a1, a2, a3, .. an } F(0,i) = ai [Base case] F(1,i) = F(0,i) xor F(0,i+1) xor F(0,i+2) ... xor F(0,n) F(2,i) = F(1,i) xor F(1,i+1) xor F(1,i+2) ... xor F(1,n) . . F(x,i) = F(x-1,i) xor F(x-1,i+1) xor F(x-1,i+2) ... xor F(x-1,n) 0 < x < 10^18 0 < n < 10^5 I need to find F(x,0) . I am trying to solve this equation for the past 3 days. I have failed to optimise it and come up with a feasible solution. Any help

Maximum of three integers using bitwise operations? [duplicate]

安稳与你 提交于 2019-12-11 01:58:17
问题 This question already has answers here : Explain this snippet which finds the maximum of two integers without using if-else or any other comparison operator? (19 answers) Closed 5 years ago . How would I return the maximum of three unsigned integers using only bit-wise operations, such as !,~,|,&,^,+,>>,<<. I don't know where to start. Any help would be appreciated. edit: I am only allowed to use the given legal ops, thats it. /** maxOfThree - Returns the maximum of three integers. * NOTE: x,

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,

How does the compiler implement bit field arithmetics?

╄→гoц情女王★ 提交于 2019-12-11 01:49:49
问题 When asking a question on how to do wrapped N bit signed subtraction I got the following answer: template<int bits> int sub_wrap( int v, int s ) { struct Bits { signed int r: bits; } tmp; tmp.r = v - s; return tmp.r; } That's neat and all, but how will a compiler implement this? From this question I gather that accessing bit fields is more or less the same as doing it by hand, but what about when combined with arithmetic as in this example? Would it be as fast as a good manual bit-twiddling

Bit duplication from 8-bit to 32-bit

扶醉桌前 提交于 2019-12-11 01:44:31
问题 I'm trying to duplicate an 8-bit value to 32-bit and wanted to ask if it's possible to write a single line algorithm to duplicate the bit values. For example: 1100 1011 -> 1111 1111 0000 0000 1111 0000 1111 1111 If it's possible, I would like to understand what's the logic behind it. 回答1: It's simple - solve the simplest case, then do more complex ones. You just need to spread bits by inserting 3 zero bits between then. Once this is done, the final step is: x = (x << 0) | (x << 1) | (x << 2)

Right shift (Division) -> ROUND TOWARD ZERO

帅比萌擦擦* 提交于 2019-12-11 00:54:59
问题 I am doing this.. value >> 3; It is always going toward negative side.How do I round toward zero with right shift division? 回答1: Do something conditionally depending on whether your value is positive or negative. if( value < 0 ) { -((-value) >> 3); } else { value >> 3; } 回答2: Gez, the answers were pretty bad ; you want to solve that without branching, but without breaking your positive numbers either. Here it is : (int)(value+(((unsigned)value)>>31)) >> 3 The cast to (unsigned) is required to

How to compare two bit values in C?

妖精的绣舞 提交于 2019-12-11 00:14:29
问题 I've been dabbling around a bit with C and I find that being able to directly manipulate bits is fascinating and powerful (and dangerous I suppose). I was curious as to what the best way would be to compare different bits in C would be. For instance, the number 15 is represented in binary as: 00001111 And the number 13 is represented as: 00001101 How would you compare what bits are different without counting them? It would be easy to use shifts to determine that 15 contains 4 1s and 13

sign function in C using bit operators only

拟墨画扇 提交于 2019-12-10 21:55:34
问题 I'm trying to implement a sign function using only bitwise operators. I know that if I just want to extract the sign bit of a signed integer, I can do: (x >> 31) & 1 . Also, I understand that conditionals can be written as boolean expressions: if(x) a=y else a=z which is equivalent to a = x ? y:z can be rewritten as: a=( (x<<31) << 31 ) & y + ( !x << 31) >> 31) & z , assuming x=1 or 0. This problem gets a little tricky though because I have 3 conditional scenarios: return 1 if positive, 0 if