bitmask

Combining Enum Value using Bitmask

蹲街弑〆低调 提交于 2019-12-04 11:58:26
问题 I understand it's possible to use bitmasks in enum values, but I don't know how to create it. I have a simple enum : enum State { minimizing = 0, maximizing, minimized, maximized }; A state is always State.minimized or State.maximized , and can have additional state on resize. So something can be Maximized and minimizing 回答1: I am going to assume that myState has the type of your enum State . The traditional use of enum is to create the constant values that a variable of this type can take.

Creating a mask with N least significant bits set

萝らか妹 提交于 2019-12-04 03:05:20
问题 I would like to create a macro or function 1 mask(n) which given a number n returns an unsigned integer with its n least significant bits set. Although this seems like it should be a basic primitive with heavily discussed implementations which compile efficiently - this doesn't seem to be the case. Of course, various implementations may have different sizes for the primitive integral types like unsigned int , so let's assume for the sake of concreteness that we are talking returning a uint64

Is there any difference between integer and bit(n) data types for a bitmask?

人走茶凉 提交于 2019-12-03 23:28:34
I am working with a table in a PostgreSQL database that has several boolean columns that determine some state (e.g. published , visible , etc.). I want to make a single status column that will store all these values as well as possible new ones in a form of a bitmask. Is there any difference between integer and bit(n) in this case? This is going to be a rather big table, because it stores objects that users create via a web-interface. So I think I will have to use (partial) indexes for this column. Erwin Brandstetter If you only have a few variables I would consider keeping separate boolean

Storing flags in a DB

那年仲夏 提交于 2019-12-03 21:38:19
问题 In my application, I'd like the user to select his working days. then store them in the database. Of course my application will process the users' data like: Is today a working day for a specific user, who are the users that should work today , ... etc. My question is, what is the best practice for doing this? should I use: Bitmasking field in the users table Many to many relationships tables by creating table for days, users and days_users. Thank you in advance. 回答1: I would say that bit

create a permission bit mask in java

拥有回忆 提交于 2019-12-03 18:59:53
问题 I want to do something like this: public enum Permissions { CanBlah1, CanBlah2, CanBlah3 } byte[] userPerm = Permissions.CanBlah1 | Permissions.CanBlah2; // check permssions // if(userPerm && Permissions.CanBlah1 == Permissions.CanBlah1) { // do something } Can you do this in Java like that? (I'm coming from a c# background) 回答1: You can easily do it using EnumSet import java.util.EnumSet; import static java.util.EnumSet.of; import static java.util.EnumSet.range; import static so.User

How to create mask with least significat bits set to 1 in C

佐手、 提交于 2019-12-03 11:23:05
Can someone please explain this function to me? A mask with the least significant n bits set to 1. Ex: n = 6 --> 0x2F, n = 17 --> 0x1FFFF // I don't get these at all, especially how n = 6 --> 0x2F Also, what is a mask? The usual way is to take a 1 , and shift it left n bits. That will give you something like: 00100000 . Then subtract one from that, which will clear the bit that's set, and set all the less significant bits, so in this case we'd get: 00011111 . A mask is normally used with bitwise operations, especially and . You'd use the mask above to get the 5 least significant bits by

Using sigaction(), c

半城伤御伤魂 提交于 2019-12-03 07:47:28
I was doing a little reading about sigaction() (sources are from my course notes) and I'm not sure I understand this text: The signal mask is calculated and installed only for the duration of the signal handler. By default, the signal “sig” is also blocked when the signal occurs. Once an action is installed for a specific signal using sigaction, it remains installed until another action is explicitly requested. Does this mean that the default signal mask is restored after returning form the signal handler? Also, do I have to re-install the handler after using it, as if I was using signal() ?

update specific bit in integer column

北城余情 提交于 2019-12-03 05:46:48
问题 I have a mysql table where user permissions are stored as a bitmask: |user | permissions | | Admin | 31 | | User | 13 | 16 8 4 2 1 Admin -> 1 1 1 1 1 -> 16 + 8 + 4 + 2 + 1 -> 31 User -> 0 1 1 0 1 -> 8 + 4 + 1 -> 13 Now I want to add a permission for every user with an sql query. Let's say I want to add the permission 16 for everyone without modifying another bit. UPDATE users SET permission = ???? How do I do this? 回答1: To add permission 16, you just say UPDATE users SET permission =

C++11 standard conformant bitmasks using enum class

偶尔善良 提交于 2019-12-03 03:08:01
问题 Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <. Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the

Number of pairs with constant difference and bitwise AND zero

放肆的年华 提交于 2019-12-02 18:24:28
问题 How to find the number of pairs whose difference is a given constant and their bitwise AND is zero? Basically, all (x,y) such that x-y = k; where k is a given constant and x&y = 0; 回答1: An interesting problem. Let k n-1 ...k 1 k 0 be the the binary representation of k . Let l be the index of the smallest i such that k i =1 We can remark that a potential pair of solutions x and y must have all their bits i , i<l at zero. Otherwise the only way to have a difference x-y with its i th bit unset