bit-manipulation

Find if every even bit is set to 0 using bitwise operators

痞子三分冷 提交于 2019-11-29 20:05:55
问题 I have a 32 bit int I can only access it 8 bits at a time. I need to find out if every even bit is set to 0 and return 0 if its true and 1 otherwise. So far I am going to split my int using shifts into 4, 8 bit variables. int a, b, c, d Now I am going to not them so now I will test if the bit is set to 1 instead of 0. To test if its set to 1 I will and them by 01010101. Now I dont know how to tell if every even bit is set to 1. I cannot use if/for/while loops or any conditional statements and

How to efficiently read bits out of bytes?

旧时模样 提交于 2019-11-29 19:13:20
问题 I'm working on a project that includes WebSockets, and data between the server (Node.js) and the client (Chrome) is sent using a custom (very simple) format for data exchange I set up. I'm sending data in pieces of 3 bits because I'm sending items which all have 8 possibilities. The data format looks like this: 0 1 bit index 01234567 8901... item aaabbbcc cddd... Currently, I'm parsing the items out of the bytes like this: var itemA = bytes[0] >> 5; var itemB = (bytes[0] >> 2) & 7; var itemC

Two elements in array whose xor is maximum

断了今生、忘了曾经 提交于 2019-11-29 18:48:45
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? templatetypedef 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 maximum value, you want to have a 1 at the highest possible position, and then of the

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

好久不见. 提交于 2019-11-29 18:43:41
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? 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...000 n & -n 0000100...000 To see why this work, consider two's complement as inverse + 1, -n == ~n + 1 n

What USEFUL bitwise operator code tricks should a developer know about? [closed]

徘徊边缘 提交于 2019-11-29 18:32:29
I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more efficiently? See the famous Bit Twiddling Hacks Most of the multiply/divide ones are unnecessary - the compiler will do that automatically and you will just confuse people. But there are a bunch of, 'check/set/toggle bit N' type hacks that are very useful if you work with hardware or communications protocols. Using bitwise operations on strings (characters)

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

孤街醉人 提交于 2019-11-29 18:17:21
问题 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 回答1: Any decent compiler will implement this bit manipulation if you just prepend a

Int to Binary Conversion explanation

耗尽温柔 提交于 2019-11-29 18:08:40
My question is based on this post: Decimal to Binary and it's chosen solution. I can get the chosen answer code working, but it only works for 5 bits. How do I modify this code to work for larger numbers, say 8 bits? I tried just adjusting the character offset in the fist line from 5 to 8, but no success. void getBin(int num, char *str) { *(str+5) = '\0'; int mask = 0x10 << 1; while(mask >>= 1) *str++ = !!(mask & num) + '0'; } and test with the given code, again adjusting the 6 to 9 to match the function above: int main() { char str[6]; getBin(10, str); printf("%s\n", str); return 0; } but the

Perform logical shift using arithmetic shift operator in C [duplicate]

那年仲夏 提交于 2019-11-29 17:28:20
This question already has an answer here: Implementing Logical Right Shift in C 8 answers Right now I am reading the book Computer Systems : Programmer Perspective. One problem in the book says to perform a logical right shift on a signed integer, I can't figure out how to start on this. The following is the actual question from the book: Fill in code for the following C functions. Function srl performs a logical right shift using an arithmetic right shift (given by value xsra ), followed by other operations not including right shifts or division. Function sra performs an arithmetic right

How do I check, if bitmask contains bit?

╄→гoц情女王★ 提交于 2019-11-29 17:17:23
问题 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? 回答1: well if (8 & bitmask == 8 ) { } will check if the

Extracting rightmost N bits of an integer

℡╲_俬逩灬. 提交于 2019-11-29 16:41:16
问题 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