bit-manipulation

How to set bits of a bit vector efficiently in parallel?

自闭症网瘾萝莉.ら 提交于 2019-12-01 03:17:18
Consider a bit vector of N bits in it ( N is large) and an array of M numbers ( M is moderate, usually much smaller than N ), each in range 0..N-1 indicating which bit of the vector must be set to 1 . The latter array is not sorted. The bit vector is just an array of integers, specifically __m256i , where 256 bits are packed into each __m256i structure. How can this work be split efficiently accross multiple threads? Preferred language is C++ (MSVC++2017 toolset v141), assembly is also great. Preferred CPU is x86_64 (intrinsics are ok). AVX2 is desired, if any benefit from it. Let's assume you

How to efficiently transpose a 2D bit matrix

吃可爱长大的小学妹 提交于 2019-12-01 03:11:49
问题 I keep stumbling over this problem (e.g. in this question). Given a 2D bit matrix/board/array in the form of an array of primitive integer types, e.g. an array of long . For simplicity, we can assume a square matrix, e.g., an array of 64 long values on platforms that have 64 bit long . Let x[i] for 0 <= i < 64 be the input array. Compute an array y[i] for 0 <= i <= 64 such that: (x[i] >> j) & 1 == (y[j] >> i) & 1 Here x >> i is the bitwise right-shift of x by i bits, & is bitwise and, and x[i

How would you set and clear a single bit in Go?

我的未来我决定 提交于 2019-12-01 03:05:22
In Golang, how do you set and clear individual bits of an integer? For example, functions that behave like this: clearBit(129, 7) // returns 1 setBit(1, 7) // returns 129 Here's a function to set a bit. First, shift the number 1 the specified number of spaces in the integer (so it becomes 0010, 0100, etc). Then OR it with the original input. This leaves the other bits unaffected but will always set the target bit to 1. // Sets the bit at pos in the integer n. func setBit(n int, pos uint) int { n |= (1 << pos) return n } Here's a function to clear a bit. First shift the number 1 the specified

Exclusion constraint on a bitstring column with bitwise AND operator

天涯浪子 提交于 2019-12-01 03:02:46
问题 So I was just reading about Exclusion Constraints in PostgreSQL and I couldn't seem to find a way to use bitwise operators on bitstrings, and I was wondering if it was possible. My use case is I have a name: text column and a value: bit(8) column. And I wanted to create a constraint that basically says this: ADD CONSTRAINT route_method_overlap EXCLUDE USING gist(name WITH =, value WITH &) But this doesn't work since operator &(bit,bit) is not a member of operator family "gist_bit_ops" I

What does this sign exactly mean? |=

Deadly 提交于 2019-12-01 02:58:26
问题 |= I'm curious to learn about this operator, I've seen this notation used while setting flags in Java. for example: notification.flags |= Notification.FLAG_AUTO_CANCEL; Does it perform some kind of bit manipulation? What does this mark exactly do? Are there any other well known signs similar to this? 回答1: It is equivalent to notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL; where | is bitwise OR operator which OR the two variables bit-by-bit. It is well known by itself.

Format negative integers in two's complement representation

故事扮演 提交于 2019-12-01 02:41:03
问题 I would like to represent an negative integer in bits, using two's complement representation. Using standard Python bit representation utilities doesn't help much: >>> bin(-5) '-0b101' >>> format(-5, 'b') '-101' -5 in two's complement is represented as 1011 . How do I do this? 回答1: Python's integers already use two's complement, but since they have arbitrary precision, the binary representation of negative numbers would have an infinite string of 1s at the start, much like positive numbers

Performance of integer and bitwise operations on GPU

浪尽此生 提交于 2019-12-01 02:32:29
Though GPUs are supposed for use with floating point data types, I'd be interested in how fast can GPU process bitwise operations. These are the fastest possible on CPU, but does GPU emulate bitwise operations or are they fully computed on hardware? I'm planning to use them inside shader programs written with GLSL. Also I'd suppose that if bitwise operations have full preformance, integer data types should have also, but I need confirmation on that. To be more precise, targeted versions are OpenGL 3.2 and GLSL 1.5. Hardware that should run this is any Radeon HD graphics card and GeForce series

How does this function compute the absolute value of a float through a NOT and AND operation?

半腔热情 提交于 2019-12-01 02:08:55
问题 I am trying to understand how the following code snippet works. This program uses SIMD vector instructions (Intel SSE) to calculate the absolute value of 4 floats (so, basically, a vectorized "fabs()" function). Here is the snippet: #include <iostream> #include "xmmintrin.h" template <typename T> struct alignas(16) sse_t { T data[16/sizeof(T)]; }; int main() { sse_t<float> x; x.data[0] = -4.; x.data[1] = -20.; x.data[2] = 15.; x.data[3] = -143.; __m128 a = _mm_set_ps1(-0.0); // ??? __m128 xv

find the index of the highest bit set of a 32-bit number without loops obviously

折月煮酒 提交于 2019-12-01 01:53:11
Here's a tough one(atleast i had a hard time :P): find the index of the highest bit set of a 32-bit number without using any loops. With recursion: int firstset(int bits) { return (bits & 0x80000000) ? 31 : firstset((bits << 1) | 1) - 1; } Assumes [31,..,0] indexing Returns -1 if no bits set | 1 prevents stack overflow by capping the number of shifts until a 1 is reached (32) Not tail recursive :) Floor of logarithm-base-two should do the trick (though you have to special-case 0). Floor of log base 2 of 0001 is 0 (bit with index 0 is set). " " of 0010 is 1 (bit with index 1 is set). " " of

How to swap nybbles in C?

自作多情 提交于 2019-12-01 01:18:35
How to swap the nybble bit positions of a number? For example: 534, convert it into binary, the rightmost 4 bits has to be interchanged with the leftmost 4 bits and then make a new number with that. Anyone know how to do this? Start from the fact that hexadecimal 0xf covers exactly four bits. There are four nibbles in a 16-bit number. The masks for the nibbles are 0xf000 , 0xf00 , 0xf0 , and 0xf . Then start masking, shifting and bitwise OR-ing. Sean Anderson's bit twiddling guide has the following: // swap nibbles ... v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4); under the entry for