bit-manipulation

Bit manipulation, permutate bits

六月ゝ 毕业季﹏ 提交于 2019-12-04 09:05:49
I am trying to make a loop that loops through all different integers where exactly 10 of the last 40 bits are set high, the rest set low. The reason is that I have a map with 40 different values, and I want to sum all different ways ten of these values can be multiplied. (This is just out of curiosity, so it's really the "bitmanip"-loop that is of interest, not the sum as such.) If I were to do this with e.g. 2 out of 4 bits, it would be easy to set all manually, 0011 = 3, 0101 = 5, 1001 = 9, 0110 = 6, 1010 = 10, 1100 = 12, but with 10 out of 40 I can't seem to find a method to generate these

Odd bit operator in the increment statement of a for loop [duplicate]

故事扮演 提交于 2019-12-04 08:52:03
问题 This question already has answers here : meaning of (number) & (-number) (3 answers) Closed 4 years ago . Given this for loop: for(++i; i < MAX_N; i += i & -i) what is it supposed to mean? What does the statement i += i & -i accomplish? 回答1: This loop is often observed in binary indexed tree (or BIT) implementation which is useful to update range or point and query range or point in logarithmic time. This loop helps to choose the appropriate bucket based on the set bit in the index. For more

Is there an elegant way to Invert a Bit value in an SQL insert Statement?

风格不统一 提交于 2019-12-04 08:47:37
问题 I'm converting some data in SQL Server: INSERT INTO MYTABLE (AllowEdit) (Select PreventEdit from SOURCETABLE) so I need to inverse the bit value from source table. I expected NOT to work, as this is how I would do it in code, but it doesn't. The most elegant way I can think of is: INSERT INTO MYTABLE (AllowEdit) (Select ABS(PreventEdit -1) from SOURCETABLE) Is there a more standard way to do it? 回答1: I did not test this myself, but you should be able to use the bitwise negation operator, ~ on

Change to HashMap hash function in Java 8

霸气de小男生 提交于 2019-12-04 08:46:11
问题 In java 8 java.util.Hashmap I noticed a change from: static int hash(int h) { h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); to: static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); It appears from the code that the new function is a simpler XOR of the lower 16 bits with the upper 16 leaving the upper 16 bits unchanged, as opposed to several different shifts in the previous implementation, and from the comments that this is

Subtracting two numbers without using '-' operator

别等时光非礼了梦想. 提交于 2019-12-04 08:38:58
问题 i tried with the following code , but i can't understand why it's giving me wrong answer. i am computing the 2's complement and adding with another no. #include <stdio.h> int add(int a, int b) { while (a) { a = (a & b) << 1; b = a^b; } return b; } int sub(int a, int b) // add a with b's 2's complement. { return (add(a, add(~b, 1))); } int main() { int a, b, res; a = 3, b = 1; res = sub(a, b); printf("%d\n", res); return 0; } 回答1: i used a different add() function as suggested by

Can the xor-swap be extended to more than two variables?

不羁的心 提交于 2019-12-04 07:25:43
I've been trying to extend the xor-swap to more than two variables, say n variables. But I've gotten nowhere that's better than 3*(n-1) . For two integer variables x1 and x2 you can swap them like this: swap(x1,x2) { x1 = x1 ^ x2; x2 = x1 ^ x2; x1 = x1 ^ x2; } So, assume you have x1 ... xn with values v1 ... vn . Clearly you can "rotate" the values by successively applying swap: swap(x1,x2); swap(x2,x3); swap(x3,x4); ... swap(xm,xn); // with m = n-1 You will end up with x1 = v2 , x2 = v3 , ..., xn = v1 . Which costs n-1 swaps, each costing 3 xors, leaving us with (n-1)*3 xors. Is a faster

Algorithm for bit expansion/duplication?

社会主义新天地 提交于 2019-12-04 06:57:30
Is there an efficient (fast) algorithm that will perform bit expansion/duplication? For example, expand each bit in an 8bit value by 3 (creating a 24bit value): 1101 0101 => 11111100 01110001 11000111 The brute force method that has been proposed is to create a lookup table. In the future, the expansion value may need to be variable. That is, in the above example we are expanding by 3 but may need to expand by some other value(s). This would require multiple lookup tables that I'd like to avoid if possible. There is a chance to make it quicker than lookup table if arithmetic calculations are

Python Bitshift 32 Bit Constraint [duplicate]

跟風遠走 提交于 2019-12-04 06:17:32
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should be a relatively easy answer, I just don't really know how to search for it...I got a few semi-relevant things, but nothing that fits what I'm trying to do. >>> 1171855803 << 7 149997542784L # I want -326312576 In other words, treat the number as an integer and don't allow it to convert to a long. How would I do this? I tried the solution

Find if x is bigger than y using bitwise operator in C [closed]

喜夏-厌秋 提交于 2019-12-04 06:02:18
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . If x > y, then this function will return 1, other wise return 0. so far i have int isitGreater(int x, int y) { return (((y+((~x)+1)) >> 31) & 1); but it

Binary matrix multiplication bit twiddling hack

拟墨画扇 提交于 2019-12-04 05:21:08
Abstract Hi, suppose you have two different independent 64-bit binary matrices A and T ( T is a transposed version of itself, using the transposed version of matrix allows during multiplication to operate on T 's rows rather than columns which is super cool for binary arithmetic) and you want to multiply these matrices the only thing is that matrix multiplication result is truncated to 64-bits and if you yield to a value greater that 1 in some specific matrix cell the resulting matrix cell will contain 1 otherwise 0 Example A T 00000001 01111101 01010100 01100101 10010111 00010100 10110000