bitmask

Can LINQ (to SQL) do bitwise queries?

I have a table of Users that includes a bitmask of roles that the user belongs to. I'd like to select users that belong to one or more of the roles in a bitmask value. For example: select * from [User] where UserRolesBitmask | 22 = 22 This selects all users that have the roles '2', '4' or '16' in their bitmask. Is this possible to e...

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

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 Da...

Is there a practical limit to the size of bit masks?

There's a common way to store multiple values in one variable, by using a bitmask. For example, if a user has read, write and execute privileges on an item, that can be converted to a single number by saying read = 4 (2^2), write = 2 (2^1), execute = 1 (2^0) and then add them together to get 7. I use this technique in several web applic...

Using SQL to determine cidr value of a subnet mask

I'd like to find a way to do a SQL query that will calculate the cidr (bit representation) of a subnet mask stored in the database. So for example, I've got either 255.255.255.0 or its decimal value (4294967040) stored in the database. I'd like to do a select and get back /24 representation via the query. I've done things like the fol...

Can I allocate a specific number of bits in C?

Hello, I am trying to store a large amount of boolean information that is determined at run-time. I was wondering what the best method might be. I have currently been trying to allocate the memory using: pStatus = malloc((<number of data points>/8) + 1); thinking that this will give me enough bits to work with. I could then refer...

Programatically calculate the size of a value type

I'm writing a unit test for a method that packs boolean values into a byte. The various bit locations are determined by the value of an enum, which only has 5 values right now, but it's conceivable (though extremely unlikely) that this number could go to 9. I'd like a simple test along the lines of: private byte m_myNum; enum MyEnum {...

What is the fastest way(s) to loop through a large data chunk on a per-bit basis.

I am running through a memory block of binary data byte-wise. Currently I am doing something like this: for (i = 0; i < data->Count; i++) { byte = &data->Data[i]; ((*byte & Masks[0]) == Masks[0]) ? Stats.FreqOf1++; // syntax incorrect but you get the point. ((*byte & Masks[1]) == Masks[1]) ? Stats.FreqOf1++; ((*byte ...

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

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 ...

Bitmask to Array Index

Hello, Is there a simple way to convert a bitmask in to an array index? ie. If i've got an enum a = 0x01, b = 0x02, c = 0x04, d = 0x08, e = 0x10, etc and I want to store releated data in an array, is there a simple way such that i can convert a to 0, b to 1, c to 2. etc? Many thanks ...

SQL Server: varbinary or int to store a bit mask?

Is there any advantage of using int vs varbinary for storing bit masks in terms of performance or flexibility. For my purposes, I will always be doing reads on these bit masks (no writes or updates). ...

Count bits in the number.

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 whi...

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

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 a...

Bit masking an index to an array, Arduino environment

I'm trying to take a 16 bit unsigned integer from a structure, mask the first 8 bits of it, and use it as an index to an array with the function analogWrite which takes the output pin on the Arduino and the output byte as arguments. The code in question looks something like this: analogWrite(outputPin, my_array[myStructure->data & 0xFF0...

interleaving bits with a mask

inputs: arbitrary bitset, e.g. bit positions 012345 arbitrary bit mask, e.g. (x=1) xx0x0x output: xx0x1x2345 That is, I want the first bit of the bitset to be placed in the first 0 of the mask. Likewise, the second bit is placed in the second 0 of the mask. example: mask = 1001001 bits = 1101 result = 1111011 I know that this ...

Why should I use bitwise/bitmask in PHP?

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 ...

why does this work? (finding odd number in c++)

for (unsigned int i = 1; i <= 100; i++) { if (i & 0x00000001) { std::cout << i<<","; } } why does (and how): if( i & 0x00000001 ) figure out the odd number? ...

convert a two Byte bit mask into a EnumSet

I am reading a binary file that has values stored in bit masks, both 1 Byte bit masks and 2 Byte bit masks. Each bit in the masks act as a switch that indicates where an Event has transpired. Example of 1 Byte mask: 00000101 Indicates that Event one and Event 3 has transpired. Example of Enum public enum MyEnum { ...

one sql parameter to give me 3 potential values

In my stored procedure I need a single parameter that will give me a potential of 3 values. So I can do something like: @p1 <-- my parameter IF (@p1 ???) -- include users SELECT * FROM USERS IF (@p1 ???) -- include employees SELECT * FROM employees IF (@p1 ???) -- iclude customers SELECT * FROM CUSTOMERS I am gues...

How to store several states in one variable?

Hi, My object Item has several binary states which can be combined bool CanBeSold; bool CanBeBought; bool CanBeExchanged; I need to store current combination of values into one variable. The reason is that I need to store this value in DB. In C++ I would create a bit-mask where one state occupies some bit. Is it good practice in .NE...

Using _bitmask from PltScheme FFI

This is a part of a plt-scheme wrapper library: (define InputMask (_bitmask '(NoEventMask = #x00000000 KeyPressMask = #x00000001 KeyReleaseMask = #x00000002 ... OwnerGrabButtonMask = #x01000000) _long)) The thing is I cant figure out ho...