bit-manipulation

Bit Twiddling in C - Counting Bits

偶尔善良 提交于 2019-12-01 11:39:00
问题 I want to count the bits that are set in an extremely large bit-vector (i.e. 100,000 bits). What I am currently doing is using a pointer to char (i.e. char *cPtr) to point to the beginning of the array of bits. I then: 1. look at each element of the array (i.e. cPtr[x]), 2. convert it to an integer (i.e. (int) cPtr[x]) 3. use a 256 element look-up table to see how many bits are set in the given byte (i.e. cPtr[x]). It occurs to me that if I use a short int pointer instead (i.e. short int *

Define BIT0, BIT1, BIT2, etc Without #define

我只是一个虾纸丫 提交于 2019-12-01 11:20:30
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 such as if (flagVariable & MOTOR_UP) { // do something clrBit(flagVariable, MOTOR_UP); } Is there a type

XOR bitset when 2D bitset is stored as 1D

隐身守侯 提交于 2019-12-01 11:10:43
问题 To answer How to store binary data when you only care about speed?, I am trying to write some to do comparisons, so I want to use std::bitset . However, for fair comparison, I would like a 1D std::bitset to emulate a 2D. So instead of having: bitset<3> b1(string("010")); bitset<3> b2(string("111")); I would like to use: bitset<2 * 3> b1(string("010111")); to optimize data locality. However, now I am having problem with How should I store and compute Hamming distance between binary codes?, as

Intrinsic to count trailing zero bits in 64-bit integers?

不问归期 提交于 2019-12-01 11:09:08
this is sort of a follow up on some previous questions on bit manipulation. I modified the code from this site to enumerate strings with K of N bits set (x is the current int64_t with K bits set, and at the end of this code it is the lexicographically next integer with K bits set): int64_t b, t, c, m, r,z; b = x & -x; t = x + b; c = x^t; // was m = (c >> 2)/b per link z = __builtin_ctz(x); m = c >> 2+z; x = t|m; The modification using __builtin_ctz() works fine as long as the least significant one bit is in the lower DWORD of x, but if is not, it totally breaks. This can be seen with the

What does '<<' mean in C?

北城余情 提交于 2019-12-01 11:07:32
问题 what does this mean? #define WS_RECURSIVE (1 << 0) I understand that it will define WS_Recursive (1 << 0) but what does << mean? Thanks! 回答1: << is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 . It is commonly used to create flags , numbers that can be combined together with | (bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc. The reason that they

using bitwise OR in javascript to convert to integer

大城市里の小女人 提交于 2019-12-01 10:38:26
问题 we can do the following to convert: var a = "129.13"|0, // becomes 129 var b = 11.12|0; // becomes 11 var c = "112"|0; // becomes 112 This seem to work but not sure if this is a standard JS feature. Does any one have any idea if this is safe to use for converting strings and decimals to integers ? 回答1: Yes, it is standard behavior. Bitwise operators only operate on integers, so they convert whatever number they're give to signed 32 bit integer. This means that the max range is that of signed

Easiest way to convert a decimal float to bit representation manually based on IEEE 754, without using any library

99封情书 提交于 2019-12-01 10:32:22
I know there are number ways to read every bit of a IEEE 754 float using written libraries. I don't want that, and I want to be able to manually convert a decimal float to binary representation based on IEEE 754. I understand how IEEE 754 works and I am just trying to apply it. I ask this question here just want to see whether my way is normal or stupid and I am also wondering how PC does it quickly. If I am given a decimal float in a string , I need to figure out what the E is and what the M is. get the two parts out: integer part i and fraction part f . deal with f . I constantly multiple 2

Bit operations in C

倖福魔咒の 提交于 2019-12-01 09:35:39
So if I have an integer that is 32 bits. The first 28 bits (from left) are to store the size of a memory chunk, the next two are 0s and the last two are: to store the if it is the last node and then to store if it is used or not (respectively). What I am trying to do is to know how to turn the flag on and off on the isLast operation and the isUsed operation. (If we consider only the last two integers (again, we start left) then 01 would be not last and is used for example, one more example is 11 is last and is used, 00 is not last and not used.) I want to be able to turn the flags on and off

Bit operations in C

六月ゝ 毕业季﹏ 提交于 2019-12-01 09:26:12
问题 So if I have an integer that is 32 bits. The first 28 bits (from left) are to store the size of a memory chunk, the next two are 0s and the last two are: to store the if it is the last node and then to store if it is used or not (respectively). What I am trying to do is to know how to turn the flag on and off on the isLast operation and the isUsed operation. (If we consider only the last two integers (again, we start left) then 01 would be not last and is used for example, one more example is

Bitwise Leftshift (<<) strange behavior

前提是你 提交于 2019-12-01 08:52:06
问题 gcc bitwise Leftshift ( << ) strange behavior. Here is my code: #include <stdio.h> #include <string.h> void foo(int n){ printf("1<<32:%d\n", 1<<32); printf("1<<(32-n):%d\n", 1<<(32-n)); } int main(){ foo(0); } If I pass 0 as parameter, the result could be different. Compiling the source code: $gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1 main.c: In function 'foo': main.c:5:3: warning: left shift count >= width of type [enabled by default] Executing the program: $demo 1<<32:0 1<<(32-n