bitmask

efficiently find the first element matching a bit mask

烂漫一生 提交于 2019-11-28 23:26:37
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 ) time and O(1) space. Method 2: Precompute a tree of all possible masks, and at each node keep the answer

Is there way to match IP with IP+CIDR straight from SELECT query?

落爺英雄遲暮 提交于 2019-11-28 17:05:33
Something like SELECT COUNT(*) AS c FROM BANS WHERE typeid=6 AND (SELECT ipaddr,cidr FROM BANS) MATCH AGAINST 'this_ip'; So you don't first fetch all records from DB and then match them one-by one. If c > 0 then were matched. BANS table: id int auto incr PK typeid TINYINT (1=hostname, 4=ipv4, 6=ipv6) ipaddr BINARY(128) cidr INT host VARCHAR(255) DB: MySQL 5 IP and IPv type (4 or 6) is known when querying. IP is for example ::1 in binary format BANNED IP is for example ::1/64 Remember that IPs are not a textual address, but a numeric ID. I have a similar situation (we're doing geo-ip lookups),

Game Engine Collison Bitmask… Why 0x01 etc?

牧云@^-^@ 提交于 2019-11-28 10:13:01
Coming across this situation both in Sprite Kit (iOS Development) and in Cocos2d-x (which I know was pretty much the inspiration for Sprite Kit, hence why they use a lot of the same tools), I finally decided to figure out why this happens: When using a physic engine, I create a sprite, and add a physicsBody to it. For the most part, I understand how to set the category, collision, and contact bitmasks, and how they work. The problem is the actual bitmask number: SpriteKit: static const uint32_t missileCategory = 0x1 << 0; sprite.physicsBody.categoryBitMask = missileCategory; Cocos2D-X: sprite-

What is a bitmask and a mask?

偶尔善良 提交于 2019-11-28 03:57:52
On the PHP documentation about JSON it mentions the word bitmask. Wikipedia has it defined as a mask. I don't understand either bitmask or mask or how they are useful. Can some one explain these terms using layman's terms and no jargon? DJanssens Bits and Bytes In computing, numbers are internally represented in binary. This means, where you use an integer type for a variable, this will actually internally will be represented as a summation of zeros and ones. As you might know, a single bit represents one 0 or one 1 . A concatenation of eight of those bits represent a Byte, e.g. 00000101 ,

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

扶醉桌前 提交于 2019-11-28 02:34:40
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 (UIView *subview in self.subviews) { if (subview.tag == viewStatus) subview.hidden = NO; else subview

Mask and aggregate bits

妖精的绣舞 提交于 2019-11-27 23:18:08
I'm trying to efficiently execute the following task: INPUT VALUE: 01101011 MASK: 00110010 MASK RESULT: --10--1- AGGREGATED: 00000101 I hope this examples explains clearly what I'm trying to achieve. What's the best way to do this in a non-naive way? harold This operation is called compress_right or just compress , and it is moderately terrible to implement without hardware support. The non-naive code from Hacker's Delight "7–4 Compress, or Generalized Extract" to implement this function is unsigned compress(unsigned x, unsigned m) { unsigned mk, mp, mv, t; int i; x = x & m; // Clear

Shift masked bits to the lsb

筅森魡賤 提交于 2019-11-27 22:13:59
问题 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

Improve this PHP bitfield class for settings/permissions?

烈酒焚心 提交于 2019-11-27 19:30:15
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 class's functionality below. I am looking for any suggestions/code to improve this class even more so it

When is it better to store flags as a bitmask rather than using an associative table?

跟風遠走 提交于 2019-11-27 17:39:46
I’m working on an application where users have different permissions to use different features (e.g. Read, Create, Download, Print, Approve, etc.). The list of permissions isn’t expected to change often. I have a couple of options of how to store these permissions in the database. In what cases would Option 2 be better? Option 1 Use an associative table. User ---- UserId (PK) Name Department Permission ---- PermissionId (PK) Name User_Permission ---- UserId (FK) PermissionId (FK) Option 2 Store a bitmask for each user. User ---- UserId (PK) Name Department Permissions [Flags] enum Permissions

Comparing two bitmasks in SQL to see if any of the bits match

瘦欲@ 提交于 2019-11-27 17:36:05
Is there a way of comparing two bitmasks in Transact-SQL to see if any of the bits match? I've got a User table with a bitmask for all the roles the user belongs to, and I'd like to select all the users that have any of the roles in the supplied bitmask. So using the data below, a roles bitmask of 6 (designer+programmer) should select Dave, Charlie and Susan, but not Nick. User Table ---------- ID Username Roles 1 Dave 6 2 Charlie 2 3 Susan 4 4 Nick 1 Roles Table ----------- ID Role 1 Admin 2 Programmer 4 Designer Any ideas? Thanks. The answer to your question is to use the Bitwise & like this