Bitmask is a technique used to isolate specific bits in a byte in order to work only on the desired ones. They are used in IP addresses to separate the network prefix and the host number for example.
Questions tagged [bitmask]
738 questions
24
votes
4 answers
How do you set only certain bits of a byte in C without affecting the rest?
Say I have a byte like this 1010XXXX where the X values could be anything. I want to set the lower four bits to a specific pattern, say 1100, while leaving the upper four bits unaffected. How would I do this the fastest in C?

PICyourBrain
- 9,976
- 26
- 91
- 136
23
votes
5 answers
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…

raspi
- 5,962
- 3
- 34
- 51
23
votes
6 answers
bitmask question?
I have the follow:
public static final int LIMIT_ONE = 1;
public static final int TRADEABLE = (1 << 1);
public static final int SELLABLE = (1 << 2);
public static final int STORABLE = (1 << 3);
public static final int STORABLE_IN_WH = (1 <<…

Prix
- 19,417
- 15
- 73
- 132
21
votes
2 answers
SELECT users from MySQL database by privileges bitmask?
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) …

Wh1T3h4Ck5
- 8,399
- 9
- 59
- 79
20
votes
3 answers
Is there any difference between integer and bit(n) data types for a bitmask?
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…

Igor Zinov'yev
- 3,676
- 1
- 33
- 49
18
votes
8 answers
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…

JasonDavis
- 48,204
- 100
- 318
- 537
18
votes
3 answers
PHP debug_backtrace bitmask usage
Trying to understand this entry in the php manual on debug_backtrace.
I don't understand what they mean by "this parameter is a bitmask for ...."
I have done web searches on bitmasks and my head is spinning round so I have decided I don't really…

Dayo
- 12,413
- 5
- 52
- 67
17
votes
5 answers
Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how?
In C I would do this
int number = 3510;
char upper = number >> 8;
char lower = number && 8;
SendByte(upper);
SendByte(lower);
Where upper and lower would both = 54
In C# I am doing this:
int number = Convert.ToInt16("3510");
…

rollsch
- 2,518
- 4
- 39
- 65
17
votes
5 answers
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 ==…

mrblah
- 99,669
- 140
- 310
- 420
16
votes
6 answers
How to create a mask with the least significant bits set to 1 in C
How does this function work?
A mask with the least significant n bits set to 1.
Example:
n = 6 --> 0x2F, n = 17 --> 0x1FFFF // I don't get these at all, especially how n = 6 --> 0x2F
Also, what is a mask?

sebi
- 815
- 7
- 15
- 26
15
votes
4 answers
efficiently find the first element matching a bit mask
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…

cberzan
- 2,009
- 1
- 21
- 32
15
votes
2 answers
Bitmask in PHP for settings?
Bits and bitmask are something I have been struggling to understand for a while, but I would like to learn how to use them for settings and things like that in PHP.
I have finally found a class that claims to do exactly that, and as I can tell, it…

JasonDavis
- 48,204
- 100
- 318
- 537
15
votes
1 answer
How and Where to use Bit Mask in java
Please explain to me how and where to use Bit Mask in java:
I don't understand the code below:
int bitmask=1;
if ((bitmask & 1) == 1) // what it does
The other questions dint exactly answer why?

Spartan
- 3,213
- 6
- 26
- 31
15
votes
2 answers
Shift masked bits to the lsb
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…

cen
- 2,873
- 3
- 31
- 56
15
votes
3 answers
Bitmask: how to determine if only one bit is set
If I have a basic bitmask...
cat = 0x1;
dog = 0x2;
chicken = 0x4;
cow = 0x8;
// OMD has a chicken and a cow
onTheFarm = 0x12;
...how can I check if only one animal (i.e. one bit) is set?
The value of onTheFarm must be 2n, but how can I check that…

Tim
- 6,281
- 3
- 39
- 49