bitmask

SELECT users from MySQL database by privileges bitmask?

蓝咒 提交于 2019-11-30 08:19:00
I have users table and want to SELECT some rows by bitmask criteria. I'll try to explain my problem with small example. Structure of table users user_id int [primary key, auto_increment] user_email varchar(200) user_privileges int Note: It has more fields but they are irrelevant for this question. Filled table may look like this +---------+--------------------+-----------------+ | user_id | user_email | user_privileges | << binary +---------+--------------------+-----------------+ | 1 | john@example.com | 165 | 10100101 | 2 | max@example.com | 13 | 00001101 | 3 | trevor@example.com | 33 |

Count bits in the number [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 07:42:57
问题 This question already has answers here : Closed 10 years ago . Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? Suppose you have a number. Is there any way to count the bits which equals to 1 in binary representation of that number, not using iteration? I mean, is there any way to do it in constant time using some bitwise operators and masks. I need solution which will work well for both architectures 32 bit and 64 bit. Ah almost forgot, I need it for C language

create a permission bit mask in java

那年仲夏 提交于 2019-11-30 03:22:39
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) 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.Permissions.CanBlah1; import static so.User.Permissions.CanBlah2; import static so.User.Permissions.CanBlah3;

Most efficient way to extract bit flags

狂风中的少年 提交于 2019-11-29 18:52:47
问题 I have these possible bit flags. 1, 2, 4, 8, 16, 64, 128, 256, 512, 2048, 4096, 16384, 32768, 65536 So each number is like a true/false statement on the server side. So if the first 3 items, and only the first 3 items are marked "true" on the server side, the web service will return a 7. Or if all 14 items above are true, I would still get a single number back from the web service which is is the sum of all those numbers. What is the best way to handle the number I get back to find out which

How do I check, if bitmask contains bit?

╄→гoц情女王★ 提交于 2019-11-29 17:17:23
问题 I don't quite understand this whole bitmask concept. Let's say I have a mask: var bitMask = 8 | 524288; I undestand that this is how I would combine 8 and 524288 , and get 524296 . BUT, how do I go the other way? How do I check my bitmask, to see if it contains 8 and/or 524288 ? To make it a bit more complex, let's say the bitmask I have is 18358536 and I need to check if 8 and 524288 are in that bitmask. How on earth would I do that? 回答1: well if (8 & bitmask == 8 ) { } will check if the

Convert a 64 bit integer into 8 separate 1 byte integers in python

落花浮王杯 提交于 2019-11-29 13:15:33
In python, I have been given a 64 bit integer. This Integer was created by taking several different 8 bit integers and mashing them together into one giant 64 bit integer. It is my job to separate them again. For example: Source number: 2592701575664680400 Binary (64 bits): 0010001111111011001000000101100010101010000101101011111000000000 int 1: 00100011 (35) int 2: 11111011 (251) int 3: 00100000 (32) int 4: 01011000 (88) int 5: 10101010 (170) int 6: 00010110 (22) int 7: 10111110 (190) int 8: 00000000 (0) So what I would like to do is take my source number 2592701575664680373 and return an

SELECT users from MySQL database by privileges bitmask?

元气小坏坏 提交于 2019-11-29 11:59:40
问题 I have users table and want to SELECT some rows by bitmask criteria. I'll try to explain my problem with small example. Structure of table users user_id int [primary key, auto_increment] user_email varchar(200) user_privileges int Note: It has more fields but they are irrelevant for this question. Filled table may look like this +---------+--------------------+-----------------+ | user_id | user_email | user_privileges | << binary +---------+--------------------+-----------------+ | 1 | john

Count bits in the number [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-29 05:23:16
Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? Suppose you have a number. Is there any way to count the bits which equals to 1 in binary representation of that number, not using iteration? I mean, is there any way to do it in constant time using some bitwise operators and masks. I need solution which will work well for both architectures 32 bit and 64 bit. Ah almost forgot, I need it for C language or assembler is also good. There is a bit counting algorithm without a loop at http://graphics.stanford.edu/~seander/bithacks.html . Lots of bit counting algorithms

Shift masked bits to the lsb

我们两清 提交于 2019-11-29 03:40:32
When you and some data with a mask you get some result which is of the same size as the data/mask. What I want to do, is to take the masked bits in the result (where there was 1 in the mask) and shift them to the right so they are next to each other and I can perform a CTZ (Count Trailing Zeroes) on them. I didn't know how to name such a procedure so Google has failed me. The operation should preferably not be a loop solution, this has to be as fast operation as possible. And here is an incredible image made in MS Paint. This operation is known as compress right . It is implemented as part of

Is there an elegant way to store a dual relationship (i.e. user 1 and user 2 are friends)

荒凉一梦 提交于 2019-11-29 02:34:10
I've run into the same problem in two different pieces of work this month: Version 1: User 1 & User 2 are friends Version 2: Axis 1 & Axis 2 when graphed should have the quadrants colored... The problem is, I don't see an elegant way, using a RDBMS, to store and query this information. There are two obvious approaches: Approach 1: store the information twice (i.e. two db rows rows per relationship): u1, u2, true u2, u1, true u..n, u..i, true u..i, u..n, true have rules to always look for the inverse on updates: on read, no management needed on create, create inverse on delete, delete inverse