bitmask

Changing specific set of bits in a byte

自古美人都是妖i 提交于 2019-12-11 15:03:21
问题 I am working on a function that receives a byte and needs to change some of the bits in that byte. For example, the function receives: 11001011 Then I need to set the MSB to 0, its easy enough: buffer[0] &= ~(1 << 7); But then I need to set bits 6 through 3 (I refer LSB as bit 0 here) to an argument that gets supplied to the function. This argument can be an integer from 0 to 6 . The important thing is I should not change any other bits. I tried with masking and stuff but I failed miserably.

Testing if a bitmask has one and only one flag

与世无争的帅哥 提交于 2019-12-11 11:47:39
问题 I've been scouring google and stack overflow for an answer to this question and I haven't been able to explicitly find it. How would I test a bitmask to see if it has one and ONLY one flag set to it? I.E It would return false if any other flags were set inside the mask? I know that I can check to see if the mask has any flags with this. (currentFlags & state) == state I figure it's a bit more complex to check if a mask only has one flag. Every site that I've visited, that explains bitmasking,

64-bit NS_OPTIONS bitmask

别等时光非礼了梦想. 提交于 2019-12-10 17:25:17
问题 Context I'm using the NS_OPTIONS macro to create a bitmask. I've assigned it a type of NSInteger and since I'm building on a 64-bit platform, that should give me a total of 63 "slots" to work with (64 bits less one bit for signed-ness). Here is the enumeration: typedef NS_OPTIONS(NSInteger, LPSVGOPlugin) { LPSVGOPluginNone = 0, LPSVGOPluginCleanupAttrs = 1 << 0, LPSVGOPluginRemoveDoctype = 1 << 1, LPSVGOPluginRemoveXMLProcInst = 1 << 2, LPSVGOPluginRemoveComments = 1 << 3,

Check if a char is ASCII using Bitmasks and Bit Operators in C

烈酒焚心 提交于 2019-12-10 10:13:40
问题 I need to write program that will check each individual char from stdin to see if it is an ASCII character. I know that what it needs to check is if the 8th bit (7th in code, if I remember correctly) is a 0, since ASCII only uses 7 bits, but I'm having difficulty figuring out just how exactly to make it check the specific bit. This is what I have at time of writing. #include <stdio.h> #define MASK 7 int main(void) { auto char inChar; do { inChar = getchar(); // Breaks the do-while loop if it

Matching binary patterns in C

隐身守侯 提交于 2019-12-10 04:17:00
问题 I'm currently developing a C program that needs to parse some bespoke data structures, fortunately I know how they are structured, however I'm not sure of how to implement my parser in C. Each of the structures are 32-bits in length, and each structure can be identified by it's binary signature. As an example, there are two particular structures that I'm interested in, and they have the following binary patterns (x means either 0 or 1) 0000-00xx-xxxx-xxx0 0000-10xx-10xx-xxx0 Within these

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 =

Binary searching via bitmasking?

自作多情 提交于 2019-12-08 03:17:27
问题 I have used this algorithm many times to binary search over Ints or Longs . Basically, I start from Long.MinValue and Long.MaxValue and decide to set the bit at i th position depending on the value of the function I am maximizing (or minimizing). In practice, this turns out to be faster (exactly 63*2 bitwise operations) and easier to code and avoids the many gotchas of traditional binary search implementations. Here is my algorithm in Scala: /** * @return Some(x) such that x is the largest

How can I implement forum privileges

荒凉一梦 提交于 2019-12-07 09:46:09
问题 I've started developing a forum application in PHP on my MVC Framework and I've got to the stage where I assign permissions to members (for example: READ, WRITE, UPDATE, DELETE). Now, I know I can add 5 columns under the user table in my database and set them to 1 | 0, but that to me seems like too much if I want to add other rules, like MOVE for example. And how can I dynamically assign these privileges them to users individually? I've heard of using a bitmasks, but it would be really good

Declaring masks for bitwise operations

北城余情 提交于 2019-12-07 05:45:49
问题 I'm new to low level operations like this, I'm hoping someone can point out the obvious mistake I must be making here. //Input value - 00111100 //I want to get the value of the bits at indexes 1-3 i.e 0111. byte mask = (byte)0x00001111; // This gives 17 not the 15 I'd expect byte shifted = (byte)(headerByte >> 3); //shifted is 7 as expected byte frameSizeValue = (byte)(shifted & mask); //Gives 1 not 7 It looks like the problem lies with the way the mask is defined, but I can't see how to fix

NumPy boolean array warning?

ぐ巨炮叔叔 提交于 2019-12-06 21:40:57
问题 I have a few numpy arrays, lets say a , b , and c , and have created a mask to apply to all of them. I am trying to mask them as such: a = a[mask] where mask is a bool array. It is worth noting that I have verified that len(a) = len(b) = len(c) = len(mask) And I am getting a rather cryptic sounding warning: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index 回答1: False == 0, and True == 1. If your mask is a list, and not an ndarray, you can get some