bit-manipulation

How can I shuffle bits efficiently?

余生颓废 提交于 2019-11-30 12:59:53
问题 I need to shuffle a 16 bit unsigned integer in a way that the even indexes land in the lower byte, and the odd indexes land in the upper byte. input: fedcba98 76543210 (contiguously numbered) output: fdb97531 eca86420 (even and odd separated) My code looks like this at the moment: typedef unsigned short u16; u16 segregate(u16 x) { u16 g = (x & 0x0001); u16 h = (x & 0x0004) >> 1; u16 i = (x & 0x0010) >> 2; u16 j = (x & 0x0040) >> 3; u16 k = (x & 0x0100) >> 4; u16 l = (x & 0x0400) >> 5; u16 m =

Fastest way to flip the sign of a double / float in C

ぐ巨炮叔叔 提交于 2019-11-30 12:41:40
What is the fastest way to flip the sign of a double (or float) in C? I thought, that accessing the sign bit directly would be the fastest way and found the following: double a = 5.0; *(__int64*)&a |= 0x8000000000000000; // a = -5.0 float b = 3.0; *(int*)&b |= 0x80000000; // b = -3.0 However, the above does not work for negative numbers: double a = -5.0; *(__int64*)&a |= 0x8000000000000000; // a = -5.0 Any decent compiler will implement this bit manipulation if you just prepend a negation operator, i.e. -a . Anyway, you're OR-ing the bit. You should XOR it. This is what the compilers I tested

How can you two-way bind a checkbox to an individual bit of a flags enumeration?

痴心易碎 提交于 2019-11-30 12:19:58
问题 For those who like a good WPF binding challenge: I have a nearly functional example of two-way binding a checkbox to an individual bit of a flags enumeration (thanks Ian Oakes, original MSDN post). The problem though is that the binding behaves as if it is one way (UI to DataContext, not vice versa). So effectively the check box does not initialize, but if it is toggled the data source is correctly updated. Attached is the class defining some attached dependency properties to enable the bit

bitwise operator and single ampersand [duplicate]

孤街浪徒 提交于 2019-11-30 11:47:04
Possible Duplicate: Why do we usually use || not | , what is the difference? Can I use single ampersand like & instead of a bitwise operator like && ? What kind of differences may arise and is there a specific example to illustrate this issue clearly? The single & will check both conditions always. The double && will stop after the first condition if it evaluates to false. Using two is a way of "short circuiting" the condition check if you truly only need 1 condition of 2 to be true or false. An example could be: if (myString != null && myString.equals("testing")) If myString is null , the

Replacing “==” with bitwise operators

怎甘沉沦 提交于 2019-11-30 11:24:05
问题 Using only bitwise operators (|, &, ~, ^, >>, <<) and other basic operators like +, -, and !, is it possible to replace the "==" below? int equal(int x, int y) { return x == y; } 回答1: Two numbers are equal if there is no difference between them: int equal(int x, int y){ return !(x-y); } 回答2: Remember that an XOR is the exactly same as NOT EQUALS and XNOR is exactly the same as EQUALS . So, the following will give you exactly what you want: return !(x ^ y); 回答3: The C ! operator is really just

Extracting rightmost N bits of an integer

痞子三分冷 提交于 2019-11-30 11:17:01
In the yester Code Jam Qualification round http://code.google.com/codejam/contest/dashboard?c=433101#s=a&a=0 , there was a problem called Snapper Chain. From the contest analysis I came to know the problem requires bit twiddling stuff like extracting the rightmost N bits of an integer and checking if they all are 1. I saw a contestant's(Eireksten) code which performed the said operation like below: (((K&(1<<N)-1))==(1<<N)-1) I couldn't understand how this works. What is the use of -1 there in the comparison?. If somebody can explain this, it would be very much useful for us rookies. Also, Any

How do I check, if bitmask contains bit?

泄露秘密 提交于 2019-11-30 11:16:09
I don't quite understand this whole bitmask concept. Let's say I have a mask: var bitMask = 8 | 524288; I undestand that this is how I would combine 8 and 524288 , and get 524296 . BUT, how do I go the other way? How do I check my bitmask, to see if it contains 8 and/or 524288 ? To make it a bit more complex, let's say the bitmask I have is 18358536 and I need to check if 8 and 524288 are in that bitmask. How on earth would I do that? well if (8 & bitmask == 8 ) { } will check if the bitmask contains 8. more complex int mask = 8 | 12345; if (mask & bitmask == mask) { //true if, and only if,

Why we need to add 1 while doing 2's complement

℡╲_俬逩灬. 提交于 2019-11-30 10:14:02
The 2's complement of a number which is represented by N bits is 2^N-number. For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001) 7==> 0111 1's compliment of 7==> 1000 1000 + 1 ------------- 1001 =====> (9) While calculating 2's complement of a number, we do following steps: 1. do one's complement of the number 2. Add one to the result of step 1. I understand that we need to do one's complement of the number because we are doing a negation operation. But why do we add the 1? This might be a silly question

Two elements in array whose xor is maximum

时间秒杀一切 提交于 2019-11-30 10:05:57
问题 Given an array of integers ,You have to find two elements whose XOR is maximum. There is naive approach --just by picking each element and xoring with other elements and then comparing the results to find the pair. Other than this ,Is there any efficient algorithm? 回答1: I think I have an O(n lg U) algorithm for this, where U is the largest number. The idea is similar to user949300's, but with a bit more detail. The intuition is as follows. When you're XORing two numbers together, to get the

Why if (n & -n) == n then n is a power of 2?

蓝咒 提交于 2019-11-30 10:05:47
问题 Line 294 of java.util.Random source says if ((n & -n) == n) // i.e., n is a power of 2 // rest of the code Why is this? 回答1: The description is not entirely accurate because (0 & -0) == 0 but 0 is not a power of two. A better way to say it is ((n & -n) == n) when n is a power of two, or the negative of a power of two, or zero. If n is a power of two, then n in binary is a single 1 followed by zeros. -n in two's complement is the inverse + 1 so the bits lines up thus n 0000100...000 -n 1111100