bit-manipulation

What is an XOR sum?

ぃ、小莉子 提交于 2021-01-20 16:26:19
问题 I am not sure of the precise definition of this term. I know that a bitwise XOR operation is going bit by bit and taking the XOR of corresponding bits position wise. Is this result termed the 'XOR sum'? If not, what is an XOR sum, and how do you use XOR to implement this addition? 回答1: In a bit wise XOR operation: a b a^b ----------- 0 0 0 0 1 1 1 0 1 1 1 0 XOR sum refers to successive XOR operations on integers. Suppose you have numbers from 1 to N and you have to find their XOR sum then for

What is an XOR sum?

霸气de小男生 提交于 2021-01-20 16:25:45
问题 I am not sure of the precise definition of this term. I know that a bitwise XOR operation is going bit by bit and taking the XOR of corresponding bits position wise. Is this result termed the 'XOR sum'? If not, what is an XOR sum, and how do you use XOR to implement this addition? 回答1: In a bit wise XOR operation: a b a^b ----------- 0 0 0 0 1 1 1 0 1 1 1 0 XOR sum refers to successive XOR operations on integers. Suppose you have numbers from 1 to N and you have to find their XOR sum then for

Converting intBitsToFloat in GLSL and floatBitsToInt back in Javascript

痞子三分冷 提交于 2021-01-05 06:45:55
问题 I'm trying to encode an integer identifier in GLSL 3.3 shader to a Float output using intBitsToFloat (I'm using highp output float vector), then I use readPixels to get this value into pixelData = new Float32Array(4) . Then I decode it back to Int in JS using floatToIntBits(pixelData[0]) , where var int8 = new Int8Array(4) var int32 = new Int32Array(int8.buffer, 0, 1) var float32 = new Float32Array(int8.buffer, 0, 1) var floatToIntBits = function (f) { float32[0] = f return int32[0] } And now

Converting intBitsToFloat in GLSL and floatBitsToInt back in Javascript

别说谁变了你拦得住时间么 提交于 2021-01-05 06:44:34
问题 I'm trying to encode an integer identifier in GLSL 3.3 shader to a Float output using intBitsToFloat (I'm using highp output float vector), then I use readPixels to get this value into pixelData = new Float32Array(4) . Then I decode it back to Int in JS using floatToIntBits(pixelData[0]) , where var int8 = new Int8Array(4) var int32 = new Int32Array(int8.buffer, 0, 1) var float32 = new Float32Array(int8.buffer, 0, 1) var floatToIntBits = function (f) { float32[0] = f return int32[0] } And now

PostgreSQL Bitwise operators with bit varying “cannot AND bit strings of different sizes”

霸气de小男生 提交于 2020-12-30 07:49:46
问题 I have a varying bitmask field and I want to perform a bitwise AND on it. PG::Error: ERROR: cannot AND bit strings of different sizes SELECT "groups".* FROM "groups" WHERE (read_roles_bitmask = B'0' OR read_roles_bitmask & B'10' > B'0') ( you need to have bitmasks with varying lengths in your table to get this error. ) I'm expecting the bitwise math to look like the following: 00010 & 100000010 = 00010 I've also tried casting the bitmask to a integer with no luck. Why does PostgreSQL choke on

PostgreSQL Bitwise operators with bit varying “cannot AND bit strings of different sizes”

江枫思渺然 提交于 2020-12-30 07:49:11
问题 I have a varying bitmask field and I want to perform a bitwise AND on it. PG::Error: ERROR: cannot AND bit strings of different sizes SELECT "groups".* FROM "groups" WHERE (read_roles_bitmask = B'0' OR read_roles_bitmask & B'10' > B'0') ( you need to have bitmasks with varying lengths in your table to get this error. ) I'm expecting the bitwise math to look like the following: 00010 & 100000010 = 00010 I've also tried casting the bitmask to a integer with no luck. Why does PostgreSQL choke on

Test that only a single bit is set in Flags Enum [duplicate]

∥☆過路亽.° 提交于 2020-12-23 08:31:09
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Closed 18 days ago . So I have a flags Enum public Enum test { test1 = 1, test2 = 2, test3 = 4, etc. } How can I test that one bit, and only one bit is set? I've 100% done this before but my mind is not working this am! 回答1: To check that only a single bit is set in a number, the number must (by definition) be a power of two. As such, you can use the following to test: int intVal = ((int

(x & (a - 1)) + 1 vs (x & a)

一笑奈何 提交于 2020-12-13 07:44:21
问题 I was looking over trying to understand the code for this closed question, and there's a line I don't understand, performing a bitwise AND on two integers, x and a : return (x & (a - 1)) + 1 What is the effect of the subtraction and addition in comparison with just a simple bitwise AND: return x & a I wrote a python script to compare the truth tables: print("( x, a) => ret vs x&a") for x in range(0, 5): print("") for a in range(0, 5): print("({:03b}, {:03b}) => {:03b} vs {:03b}".format(x, a,

(x & (a - 1)) + 1 vs (x & a)

岁酱吖の 提交于 2020-12-13 07:41:11
问题 I was looking over trying to understand the code for this closed question, and there's a line I don't understand, performing a bitwise AND on two integers, x and a : return (x & (a - 1)) + 1 What is the effect of the subtraction and addition in comparison with just a simple bitwise AND: return x & a I wrote a python script to compare the truth tables: print("( x, a) => ret vs x&a") for x in range(0, 5): print("") for a in range(0, 5): print("({:03b}, {:03b}) => {:03b} vs {:03b}".format(x, a,

XOR of pairwise sum of every unordered pairs in an array

别等时光非礼了梦想. 提交于 2020-12-04 05:14:32
问题 Question Description : Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. I solved this question using the method described in this post. My Code : int xorAllSum(int a[], int n) { int curr, prev = 0; int ans = 0; for (int k = 0; k < 32; k++) { int o = 0, z = 0; for (int i = 0; i < n; i++) { if (a[i] & (1 << k)) { o++; } else { z++; } } curr = o * z + prev; if (curr & 1) { ans = ans | (1 << k); } prev = o * (o - 1) / 2;