bit-masks

Storing multiple values via bitmask in c#

折月煮酒 提交于 2019-12-30 05:15:12
问题 I'm trying to store four independent 5-bit values (0-31) inside a 32-bit int via bit mask but am having trouble getting the values correct to set and get the individual values from the masked int used for storage. Can anyone help me with this? Edit: Sorry for the external link - here's some JavaScript demonstrating what I'm trying to achieve (but in bitmasks instead of decimal algebra): var s = 0; var v = [31, 6, 23, 31]; //save values s = v[0] + (v[1] * 32) + (v[2] * 1024) + (v[3] * 32768);

What is the purpose of “int mask = ~0;”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 12:03:58
问题 I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1 . So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0 ? Can we use -1 instead of ~0 ? 回答1: It's a portable way to set all the binary bits in an integer to 1 bits without having to know how many bits are in the integer on the current architecture. 回答2: C and C++ allow 3 different signed integer formats: sign-magnitude, one

replace mask with original image opencv Python

徘徊边缘 提交于 2019-12-12 04:45:27
问题 I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement image[np.where((image2 == [255,255,255].any(axis = 2)) I am stuck here and I have no idea how to change found white values to what the original image is (to use alongside other masks). I have tried image.shape and this did not work. Thanks. 回答1: Make a copy of the mask

Implement bitmask or relational ACL in PHP

江枫思渺然 提交于 2019-12-10 17:43:11
问题 I think PHP people are familiar with the E_ALL and various other bitmask constants from the error_reporting() function. They are number constants, example: E_ALL means 32676 and E_NOTICE means 8 . I can say that I want all errors but notice shown, and I do this by passing E_ALL & ~E_NOTICE as the argument of error_reporting() . But essentially, I tell it 32759 which is 32767 - 8 . These bitmasks are taking their values from the output set of f(x) = 2^x function, and doing add and subtract

Bitmask: how to determine if only one bit is set

邮差的信 提交于 2019-12-10 01:42:55
问题 If I have a basic bitmask... cat = 0x1; dog = 0x2; chicken = 0x4; cow = 0x8; // OMD has a chicken and a cow onTheFarm = 0x12; ...how can I check if only one animal (i.e. one bit) is set? The value of onTheFarm must be 2 n , but how can I check that programmatically (preferably in Javascript)? 回答1: You can count the number of bits that are set in a non-negative integer value with this code (adapted to JavaScript from this answer): function countSetBits(i) { i = i - ((i >> 1) & 0x55555555); i =

What is the optimal way to choose a set of features for excluding items based on a bitmask when matching against a large set?

旧巷老猫 提交于 2019-12-08 07:26:19
问题 Suppose I have a large, static set of objects, and I have an object that I want to match against all of them according to a complicated set of criteria that entails an expensive test. Suppose also that it's possible to identify a large set of features that can be used to exclude potential matches, thereby avoiding the expensive test. If a feature is present in the object I am testing, then I can exclude any objects in the set that don't have this feature. In other words, the presence of the

Bitmask: how to determine if only one bit is set

江枫思渺然 提交于 2019-12-04 23:13:24
If I have a basic bitmask... cat = 0x1; dog = 0x2; chicken = 0x4; cow = 0x8; // OMD has a chicken and a cow onTheFarm = 0x12; ...how can I check if only one animal (i.e. one bit) is set? The value of onTheFarm must be 2 n , but how can I check that programmatically (preferably in Javascript)? Ted Hopp You can count the number of bits that are set in a non-negative integer value with this code (adapted to JavaScript from this answer ): function countSetBits(i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >

Storing multiple values via bitmask in c#

别等时光非礼了梦想. 提交于 2019-11-30 16:07:40
I'm trying to store four independent 5-bit values (0-31) inside a 32-bit int via bit mask but am having trouble getting the values correct to set and get the individual values from the masked int used for storage. Can anyone help me with this? Edit: Sorry for the external link - here's some JavaScript demonstrating what I'm trying to achieve (but in bitmasks instead of decimal algebra): var s = 0; var v = [31, 6, 23, 31]; //save values s = v[0] + (v[1] * 32) + (v[2] * 1024) + (v[3] * 32768); console.log(s); //retrieve values v[3] = parseInt(s / 32768); v[2] = parseInt((s - (v[3] * 32768)) /

How to define category bit mask enumeration for SpriteKit in Swift?

醉酒当歌 提交于 2019-11-30 10:45:52
问题 To define a category bit mask enum in Objective-C I used to type: typedef NS_OPTIONS(NSUInteger, CollisionCategory) { CollisionCategoryPlayerSpaceship = 0, CollisionCategoryEnemySpaceship = 1 << 0, CollisionCategoryChickenSpaceship = 1 << 1, }; How can I achieve the same using Swift ? I experimented with enumerations but can't get it working. Here is what I tried so far. 回答1: What you could do is use the binary literals: 0b1 , 0b10 , 0b100 , etc. However, in Swift you cannot bitwise-OR enums,

What is the purpose of “int mask = ~0;”?

不想你离开。 提交于 2019-11-30 05:35:54
I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1 . So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0 ? Can we use -1 instead of ~0 ? It's a portable way to set all the binary bits in an integer to 1 bits without having to know how many bits are in the integer on the current architecture. C and C++ allow 3 different signed integer formats: sign-magnitude, one's complement and two's complement ~0 will produce all-one bits regardless of the sign format the system uses. So