bitmask

What to do when bit mask (flags) enum gets too large

℡╲_俬逩灬. 提交于 2019-11-27 17:09:38
I have a very large set of permissions in my application that I represent with a Flags enumeration. It is quickly approaching the practical upper bound of the long data type. And I am forced to come up with a strategy to transition to a different structure soon. Now, I could break this list down into smaller pieces, however, this is already just a subset of the overall permissions for our application, based on our applications layout. We use this distinction extensively for display purposes when managing permissions and I would rather not have to revisit that code at this time if I can avoid

How to implement a bitmask in php?

断了今生、忘了曾经 提交于 2019-11-27 17:07:46
I'm not sure if bitmask is the correct term. Let me explain: In php, the error_reporting function can be called multiple ways: // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE // This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); I got the term bitmask from the php.net page here Anyway the point of this is, I have implemented a

How to use bitmask? [closed]

一个人想着一个人 提交于 2019-11-27 17:01:32
How do i use it in C++ ? when is it useful to use ? Please give me an example of a problem where bitmask is used , how it actually works . Thanks! Bit masking is "useful" to use when you want to store (and subsequently extract) different data within a single data value. An example application I've used before is imagine you were storing colour RGB values in a 16 bit value. So something that looks like this: RRRR RGGG GGGB BBBB You could then use bit masking to retrieve the colour components as follows: const unsigned short redMask = 0xF800; const unsigned short greenMask = 0x07E0; const

Fastest way to produce a mask with n ones starting at position i

风格不统一 提交于 2019-11-27 14:47:21
What is the fastest way (in terms of cpu cycles on common modern architecture), to produce a mask with len bits set to 1 starting at position pos : template <class UIntType> constexpr T make_mask(std::size_t pos, std::size_t len) { // Body of the function } // Call of the function auto mask = make_mask<uint32_t>(4, 10); // mask = 00000000 00000000 00111111 11110000 // (in binary with MSB on the left and LSB on the right) Plus, is there any compiler intrinsics or BMI function that can help? If by "starting at pos ", you mean that the lowest-order bit of the mask is at the position corresponding

efficiently find the first element matching a bit mask

≯℡__Kan透↙ 提交于 2019-11-27 14:45:32
问题 I have a list of N 64-bit integers whose bits represent small sets. Each integer has at most k bits set to 1. Given a bit mask, I would like to find the first element in the list that matches the mask, i.e. element & mask == element . Example: If my list is: index abcdef 0 001100 1 001010 2 001000 3 000100 4 000010 5 000001 6 010000 7 100000 8 000000 and my mask is 111000 , the first element matching the mask is at index 2. Method 1: Linear search through the entire list. This takes O( N )

How do you set only certain bits of a byte in C without affecting the rest?

血红的双手。 提交于 2019-11-27 13:44:22
问题 Say I have a byte like this 1010XXXX where the X values could be anything. I want to set the lower four bits to a specific pattern, say 1100, while leaving the upper four bits unaffected. How would I do this the fastest in C? 回答1: You can set all those bits to 0 by bitwise-anding with the 4 bits set to 0 and all other set to 1 (This is the complement of the 4 bits set to 1). You can then bitwise-or in the bits as you would normally. ie val &= ~0xf; // Clear lower 4 bits. Note: ~0xf ==

Why should I use bitwise/bitmask in PHP?

社会主义新天地 提交于 2019-11-27 06:23:45
I am working on a user-role / permission system in PHP for a script. Below is a code using a bitmask method for permissions that I found on phpbuilder.com. Below that part is a much simpler version w3hich could do basicly the same thing without the bit part. Many people have recommended using bit operators and such for settings and other things in PHP, I have never understood why though. In the code below is there ANY benefit from using the first code instead of the second? <?php /** * Correct the variables stored in array. * @param integer $mask Integer of the bit * @return array */ function

Improve this PHP bitfield class for settings/permissions?

…衆ロ難τιáo~ 提交于 2019-11-27 04:22:47
问题 I have been trying to figure out the best way to use bitmask or bitfields in PHP for a long time now for different areas of my application for different user settings and permissions. The farthest I have come so far is from a class contributed by svens in the Stack Overflow post Bitmask in PHP for settings? . I have slightly modified it below, changing it to use class constants instead of DEFINE and making sure the get method is passed an int only. I also have some sample code to test the

Bitmask in PHP for settings?

痞子三分冷 提交于 2019-11-27 01:29:14
Bits and bitmask are something I have been struggling to understand for a while, but I would like to learn how to use them for settings and things like that in PHP. I have finally found a class that claims to do exactly that, and as I can tell, it seems to work, but I am not sure if it is the best way of doing this. I will post the class file with example code below to show it in working order. Please if you have experience, tell me if it can be improved, for performance or anything else. I really want to learn this, and I have been reading up on it, but it is a difficult one for me to grasp

Declaring and checking/comparing (bitmask-)enums in Objective-C

巧了我就是萌 提交于 2019-11-26 23:50:10
问题 You know in Cocoa there is this thing, for example you can create a UIView and do: view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; I have a custom UIView with multiple states, which I have defined in an enum like this: enum DownloadViewStatus { FileNotDownloaded, FileDownloading, FileDownloaded }; For each created subview, I set its tag : subview1.tag = FileNotDownloaded; Then, I have a custom setter for the view state which does the following: for