bit-manipulation

Given an array of length n, find number of subsets where XOR of a subset is equal to a given number [closed]

不打扰是莪最后的温柔 提交于 2019-11-29 12:19:05
Given an array, arr , of length n , find how many subsets of arr there are such that XOR(^) of those subsets is equal to a given number, ans . I have this dp approach but is there a way to improve its time complexity. ans is always less than 1024. Here ans is the no. such that XOR(^) of the subsets is equal to it. arr[n] contains all the numbers memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(i = 1; i <= n; i++){ for(j = 0; j < 1024; j++) { dp[i][j] = (dp[i-1][j] + dp[i-1][j^arr[i]]); } } cout << (dp[n][ans]); Mohit Jain From user3386109 's comment, building on top of your code: /* Warning:

Given XOR & SUM of two numbers. How to find the numbers?

我们两清 提交于 2019-11-29 12:05:13
Given XOR & SUM of two numbers. How to find the numbers? For example, x = a+b, y = a^b; if x,y are given, how to get a, b? And if can't, give the reason. This cannot be done reliably. A single counter-example is enough to destroy any theory and, in your case, that example is 0, 100 and 4, 96 . Both of these sum to 100 and xor to 100 as well: 0 = 0000 0000 4 = 0000 0100 100 = 0110 0100 96 = 0110 0000 ---- ---- ---- ---- xor 0110 0100 = 100 xor 0110 0100 = 100 Hence given a sum of 100 and an xor of 100 , you cannot know which of the possibilities generated that situation. For what it's worth,

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

你。 提交于 2019-11-29 11:55:49
问题 Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest way to find X given Y = 2 X in Python ?"** I am currently trying to optimize a routine (Rabin-Miller primality test) that reduces an even number N in the forms 2**s * d . I can get the 2**s part by: two_power_s = N & -N but I can't find a way to

Bit conversion tool in Objective-C

為{幸葍}努か 提交于 2019-11-29 11:39:28
Are there any built in utilities or macros in the objective-c libraries for iOS that will allow you to convert bytes to and from integers with respect to endianess? Please don't tell me to use bit-shifting operations. I am trying to avoid writing custom code to do this if it already exists. I would like the code to convert NSData* to primitive types (int, uint, short, etc) and to convert primitive types back to NSData*. You can get the bytes from NSData by accessing the bytes property. Then just cast that to a pointer to whatever type you want. Obviously you'll need to ensure you know the

I want to pack the bits based on arbitrary mask

旧巷老猫 提交于 2019-11-29 11:29:03
Let's say that data is 1011 1001 and the mask is 0111 0110 , then you have: input data: 1011 1001 input mask: 0111 0110 apply mask: 0011 0000 (based on `input mask`) bits selected: -011 -00- (based on `input mask`) right packed: ---0 1100 expected result: 0000 1100 (set left `8 - popcount('input mask')` bits to zero) So the final output is 0000 1100 (note that the 3 positions on the left which are unspecified are zero-filled). You can see that wherever the bits in input mask is 1 the corresponding value in input data is selected (in bits selected above) and then all selected bits are packed

How many 1s in an n-bit integer?

家住魔仙堡 提交于 2019-11-29 11:22:01
An interesting problem I ran into today: what is the fastest way to count the number of 1s in an n-bit integer? Is it possible to beat O(n)? For example: 42 = 0b101010 => 3 ones 512 = 0b1000000000 => 1 one Clearly, the naive algorithm is to simply count. But, are there any tricks to speed it up? (This is merely an academic question; there is no expected performance gain by implementing such a strategy.) See the fabulous Bit Twiddling Hacks article . Probably the fastest way on x86 processors would be to use the POPCNT class of instructions. The fastest way (without using special processor

Bit shifting a byte by more than 8 bit

对着背影说爱祢 提交于 2019-11-29 11:12:46
In here When converting from bytes buffer back to unsigned long int: unsigned long int anotherLongInt; anotherLongInt = ( (byteArray[0] << 24) + (byteArray[1] << 16) + (byteArray[2] << 8) + (byteArray[3] ) ); where byteArray is declared as unsigned char byteArray[4]; Question: I thought byteArray[1] would be just one unsigned char (8 bit). When left-shifting by 16, wouldn't that shift all the meaningful bits out and fill the entire byte with 0? Apparently it is not 8 bit. Perhaps it's shifting the entire byteArray which is a consecutive 4 byte? But I don't see how that works. In that

Set the i-th bit to zero? [duplicate]

陌路散爱 提交于 2019-11-29 11:05:46
This question already has an answer here: How do you set, clear, and toggle a single bit? 27 answers I would like to set the i-th bit to zero no matter what the i-th bit is. unsigned char pt = 0b01100001; pt[0] = 0; // its not how we do this... Setting it to one, we can use a mask pt | (1 << i) but i'm not sure how to create a mask for setting 0, if thats possible. You just have to replace the logical OR with a logical AND operation. You would use the & operator for that: pt = pt & ~(1 << i); You have to invert your mask because logical AND ing with a 1 will maintain the bit while 0 will clear

`Math.trunc` vs `|0` vs `<<0` vs `>>0` vs `&-1` vs `^0`

限于喜欢 提交于 2019-11-29 11:00:30
I have just found that in ES6 there's a new math method: Math.trunc . I have read its description in MDN article , and it sounds like using |0 . Moreover, <<0 , >>0 , &-1 , ^0 also do similar things (thanks @kojiro & @Bergi). After some tests, it seems that the only differences are: Math.trunc returns -0 with numbers in interval (-1,-0] . Bitwise operators return 0 . Math.trunc returns NaN with non numbers. Bitwise operators return 0 . Are there more differences (among all of them)? n | Math.trunc | Bitwise operators ---------------------------------------- 42.84 | 42 | 42 13.37 | 13 | 13 0

In C bits, multiply by 3 and divide by 16

走远了吗. 提交于 2019-11-29 10:28:40
问题 A buddy of mine had these puzzles and this is one that is eluding me. Here is the problem, you are given a number and you want to return that number times 3 and divided by 16 rounding towards 0. Should be easy. The catch? You can only use the ! ~ & ^ | + << >> operators and of them only a combination of 12. int mult(int x){ //some code here... return y; } My attempt at it has been: int hold = x + x + x; int hold1 = 8; hold1 = hold1 & hold; hold1 = hold1 >> 3; hold = hold >> 4; hold = hold +