bitmask

create a permission bit mask in java

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

Little Endian Bitmask

I need to convert an array of integers to a little endian bitmask using Ruby. Any links or hints would be appreciated. the example says [2,7,9,11] => "4205" a = [2,7,9,11] # 4205 b = [1,2,3,4] # 0F00 def array_to_mask(arr) mask = 0 arr.each do |i| mask = mask | (1 << i) end return mask.to_s(16) end p array_to_mask(a) # a84 ...

Fast way to get bitmask for delivery to all devices.

I have a list of devices and a bitmask of channels they are on (channels are numbered 0..3). There can be up to 256 devices. For example: Device1: 1 0 0 1 (on channels 0, 3) Device2: 0 1 1 0 (on channels 1, 2) Device3: 1 1 0 0 (on channels 2, 3) I need to find a bitmask of channels which will result in the message to be received by a...

BitMask operation in java

Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target -4 Costco -8 Bjs -16 In DB, data is stored by masking these values based on their availability for each product. eg., Mask product description 1 laptop Available in Amazon 17 iPhone Availa...

How to produce 64 bit masks?

Based on the following simple program the bitwise left shit operator works only for 32 bits. Is it true? #include <iostream> #include <stdlib.h> using namespace std; int main(void) { long long currentTrafficTypeValueDec; int input; cout << "Enter input:" << endl; cin >> input; currentTr...

Does this type of function or technique have a name?

HI there, I'm slightly new to programming, more of a hobby. I am wondering if a the following logic or technique has a specific name, or term. My current project has 7 check boxes, one for each day of the week. I needed an easy to save which boxes were checked. The following is the method to saved the checked boxes to a single number....

Efficient way to create/unpack large bitfields in C?

I have one microcontroller sampling from a lot of ADC's, and sending the measurements over a radio at a very low bitrate, and bandwidth is becoming an issue. Right now, each ADC only give us 10 bits of data, and its being stored in a 16-bit integer. Is there an easy way to pack them in a deterministic way so that the first measurement ...

Most efficient way to extract bit flags

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

Inserting bits into byte

I was looking at an example of reading bits from a byte and the implementation looked simple and easy to understand. I was wondering if anyone has a similar example of how to insert bits into a byte or byte array, that is easier to understand and also implement like the example below. Here is the example I found of reading bits from a ...

Storing int value of bitmask - extract 1 valued bits

I am calculating the int equivalent of a given set of bits and storing that in memory. From there, I would like to determine all 1 value bits from the original bitmask. Example: 33 --> [1,6] 97 --> [1,6,7] Ideas for an implementation in Java? ...

How can I implement forum privileges

I've started developing a forum application in PHP on my MVC Framework and I've got to the stage where I assign permissions to members (for example: READ, WRITE, UPDATE, DELETE). Now, I know I can add 5 columns under the user table in my database and set them to 1 | 0, but that to me seems like too much if I want to add other rules, l...

How to mask image with another image in ActionScript 3.0

Hi Gurus, my issue is this, my users import an image with FileReference and I need to mask it and then send it to server. My problem is this: I'm be able do keep the filereference event and transfer the image data into my canvas. I'm be able to send to server the result of masking. But I'm NOT be able to mask the image that my users...

Is there a name for the technique of using base-2 numbers to encode a list of unique options?

Apologies for the rather vague nature of this question, I've never formally been taught programming and Google is rather useless to a self-help guy like me in this case as the key words are pretty ambiguous. I am writing a couple of functions that encode and decode a list of options into a Long so they can easily be passed around the ap...

How can I get the bitmask value back out of an enum with the flag attribute in C#?

In our database we have a bitmask that represents what types of actions a user can make. In our C# client when we retrieve this integer value from the database we construct an enum/flag. It looks somewhat like the following: [Flags] public enum SellPermissions { Undefined = 0, Buy = 1, Sell = 2, SellOpen = 4, SellC...

Java: How do I create a Method to extract an Integer's split bits from a byte array using a mask

While working on decoding some video streaming standards I have noticed a lot of instances where the bits of an Integer value are provided in anything from 2-6 bytes but separated by reserved bits, as follows: // Specification (16 bits) // ----------------------- // Reserved 1 bit // Value A [6-7] 2 bit // Reserved ...

Using a bitmask in C#

Let's say I have the following int susan = 2; //0010 int bob = 4; //0100 int karen = 8; //1000 and I pass 10 (8 + 2) as a parameter to a method and I want to decode this to mean susan and karen I know that 10 is 1010 but how can I do some logic to see if a specific bit is checked as in if (condition_for_karen) // How to quickly che...

Searching by bitmask in ActiveRecord

I have a users table with a bitmask field that has a permissions mask in it. Locally, I can determine whether a user has a certain permission by doing a bitmask (UserPermissions&Perm)==Perm. However, I want to be able to issue a find_by_mask or something similar, perhaps using a :conditions, but I can't seem to find out how I can query t...

How do I compare (AND) two strings which represent a binary mask in mysql?

I have a table in mysql which stores (among other columns) a bitmask as a string, for example: 000100 I'd like to perform a query which will AND these fields together to provide a result that shows when any two mask positions are both true. As an example, consider these three sample records: id name mask == ==== ==== 11 a 00...

Finding duplicate records in mysql based on a bitmask.

I have a mysql table which stores maintenance logs for sensors. I'd like to design a query that finds instances where a given sensor was repaired/maintained for the same reason. (Recurring problem finder.) My table (simplified) looks like this: id name mask == ==== ==== 11 alpha 0011 12 alpha 0010 13 alpha ...

Given a list and a bitmask, how do I return the values at the indices that are True?

I start with the following list s and bitmask b: s = ['baa', 'baa', 'black', 'sheep', 'have', 'you', 'any', 'wool'] b = [1, 0, 0, 0, 1, 1, 1, 0] # or any iterable with boolean values How do I write some function apply_bitmask(s, b) so that it returns ['baa', 'have', 'you', 'any'] ...