bit-manipulation

How to set multiple bits in one line in C?

痞子三分冷 提交于 2019-12-01 08:45:27
I'd write two lines to set, say, some bits to something. Here, for example, I want to set upper 8 bits in uint16_t value x to y's lower 8 bits. uint16_t y = 0x0034; uint16_t x = 0xFF12; I want to have x: assert(x == 0x3412); I tend to write these two lines: x &= 0x00FF; x |= (y << 8); Is there a way of writing a single line to achieve the same effect without using macro? clcto Just expand out the two lines: x &= 0x00FF; // x = x & 0xFF x |= (y<<8); // x = x | (y<<8) // and combine x = (x & 0xFF) | (y << 8); 来源: https://stackoverflow.com/questions/21786843/how-to-set-multiple-bits-in-one-line

Define BIT0, BIT1, BIT2, etc Without #define

最后都变了- 提交于 2019-12-01 08:07:48
问题 Is it possible in C++ to define BIT0, BIT1, BIT2 in another way in C++ without using #define? #define BIT0 0x00000001 #define BIT1 0x00000002 #define BIT2 0x00000004 I then take the same thing and make states out of those bits: #define MOTOR_UP BIT0 #define MOTOR_DOWN BIT1 Note: I am using 32 bits only, not 64 bits. I am also using a setBit(flagVariable, BIT) (consequently a clrBit macro to do the opposite) macro to set the bits then compare whether the bit is set using the bitwise operator

C - Algorithm for Bitwise operation on Modulus for number of not a power of 2

别等时光非礼了梦想. 提交于 2019-12-01 07:34:49
I know that modulo of power of 2 can be calculated using bitwise operator x % 2^n == x & (2^n - 1). But I am wondering is there any generalized bitwise algorithm exists to find the modulus of any number is not a power of 2. For example, 7%5 Thank you in advance. There are a couple, for special cases, including 5. Since 16 ≡ 1 (mod 5), a trick you could do is split your variable into 4-bit nibbles, look up the modulus of each nibble in a table, and add the values together to get the modulus of the original number. This program uses bitfields, table lookups, and addition. It would also work for

How to efficiently transpose a 2D bit matrix

﹥>﹥吖頭↗ 提交于 2019-12-01 07:32:38
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] is the value at i th position in array x . How to implement a function that maps array x to array y

Simple bitwise manipulation for little-endian integer, in big-endian machine?

本小妞迷上赌 提交于 2019-12-01 07:04:17
For a specific need I am building a four byte integer out of four one byte chars, using nothing too special (on my little endian platform): return (( v1 << 24) | (v2 << 16) | (v3 << 8) | v4); I am aware that an integer stored in a big endian machine would look like AB BC CD DE instead of DE CD BC AB of little endianness, although would it affect the my operation completely in that I will be shifting incorrectly, or will it just cause a correct result that is stored in reverse and needs to be reversed? I was wondering whether to create a second version of this function to do (yet unknown) bit

Set individual bit in C++

烂漫一生 提交于 2019-12-01 06:32:27
I have a 5 byte data element and I need some help in figuring out how in C++ to set an individual bit of one of these byte; Please see my sample code below: char m_TxBuf[4]; I would like to set bit 2 to high of byte m_TxBuf[1] . 00000 0 00 ^ This one Any support is greatly appreciated; Thanks! Bitwise operators in C++ . "...set bit 2..." Bit endianness . I would like to set bit 2 to high of byte m_TxBuf[1]; m_TxBuf[1] |= 1 << 2 You can use bitwise-or ( | ) to set individual bits, and bitwise-and ( & ) to clear them. int bitPos = 2; // bit position to set m_TxBuf[1] |= (1 << bitPos); m_TxBuf[1]

set individual bit in AVX register (__m256i), need “random access” operator

亡梦爱人 提交于 2019-12-01 06:21:57
问题 So, I want to set an individual bit of a __m256i register. Say, my __m256i contains: [ 1 0 1 0 | 1 0 1 0 | ... | 1 0 1 0 ] , how do I set and unset the n-th bit? 回答1: This is an implementation of function which can set individual bit inside a vector: #include <immintrin.h> #include <assert.h> void SetBit(__m256i & vector, size_t position, bool value) { assert(position <= 255); uint8_t lut[32] = { 0 }; lut[position >> 3] = 1 << (position & 7); __m256i mask = _mm256_loadu_si256((__m256i*)lut);

PHP equivalent javascript >>> shift right with zero fill bitwise operators?

不想你离开。 提交于 2019-12-01 06:20:49
May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript. I just managed to discover a function as follow: function zeroFill($a, $b) { $z = hexdec(80000000); if ($z & $a) { $a = ($a>>1); $a &= (~$z); $a |= 0x40000000; $a = ($a>>($b-1)); } else { $a = ($a>>$b); } return $a; } but unfortunately, it doesn't work perfectly. EG: -1149025787 >>> 0 Javascript returns 3145941509 PHP zeroFill() return 0 /** * The >>> javascript operator in php x86_64 * Usage: -1149025787 >>> 0 ---> rrr(-1149025787, 0) === 3145941509 * @param int $v * @param int $n *

Variable length integer encoding

ぐ巨炮叔叔 提交于 2019-12-01 06:06:54
I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an area of the decode buffer/window to be output is encoded in the file as a variable length integer. I've read as much as I can about variable length integer encoding and the method being used in this case does not appear to be like any others I have seen. Perhaps to avoid patent issues or maybe just to obfuscate. The included code might not be quite complete but it is working on at least several files at this point. I cannot see how, if at all, the formulas being used below could be reduced into something

Exclusion constraint on a bitstring column with bitwise AND operator

瘦欲@ 提交于 2019-12-01 05:33:49
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 assume this is because the bit_ops & operator doesn't return a boolean. But is there a way to do what I'm