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
282
votes
6 answers
What is bit masking?
I am fairly new to C programming, and I encountered bit masking. What is the general concept and function of bit masking?
Examples are much appreciated.

Mr.Z
- 2,929
- 3
- 14
- 6
182
votes
17 answers
What does (x ^ 0x1) != 0 mean?
I came across the following code snippet
if( 0 != ( x ^ 0x1 ) )
encode( x, m );
What does x ^ 0x1 mean? Is this some standard technique?

KodeWarrior
- 3,538
- 3
- 26
- 40
120
votes
6 answers
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…

Matt
- 25,943
- 66
- 198
- 303
96
votes
10 answers
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…

Matthew Vines
- 27,253
- 7
- 76
- 97
93
votes
4 answers
How to implement a bitmask in php?
I'm not sure if bitmask is the correct term. Let me explain:
In php, the error_reporting function can be called multiple ways:
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to…

AlexMorley-Finch
- 6,785
- 15
- 68
- 103
79
votes
4 answers
Declaring and checking/comparing (bitmask-)enums in Objective-C
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…

thibaultcha
- 1,320
- 2
- 15
- 27
78
votes
9 answers
When is it better to store flags as a bitmask rather than using an associative table?
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…

Ryan Kohn
- 13,079
- 14
- 56
- 81
78
votes
4 answers
How can I use a bitmask?
How do I use it in C++? When is it useful to use?
What would be an example of a problem where a bitmask is used to see how it actually works?

Animea Animea
- 859
- 2
- 8
- 14
68
votes
0 answers
What is a bitmask and a mask?
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?

Robert
- 10,126
- 19
- 78
- 130
58
votes
6 answers
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…

Nick
- 5,616
- 10
- 52
- 72
41
votes
6 answers
if (mask & VALUE) or if ((mask & VALUE) == VALUE)?
You're probably familiar with the enum bitmask scheme, like:
enum Flags {
FLAG1 = 0x1,
FLAG2 = 0x2,
FLAG3 = 0x4,
FLAG4 = 0x8,
NO_FLAGS = 0,
ALL_FLAGS = FLAG1 | FLAG2 | FLAG3 | FLAG4
};
f(FLAG2 | FLAG4);
I've seen a lot of…

aschepler
- 70,891
- 9
- 107
- 161
39
votes
5 answers
C++11 standard conformant bitmasks using enum class
Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r)…

B.S.
- 1,435
- 2
- 12
- 18
29
votes
9 answers
How to define category bit mask enumeration for SpriteKit in Swift?
To define a category bit mask enum in Objective-C I used to type:
typedef NS_OPTIONS(NSUInteger, CollisionCategory)
{
CollisionCategoryPlayerSpaceship = 0,
CollisionCategoryEnemySpaceship = 1 << 0,
CollisionCategoryChickenSpaceship = 1…

Rafał Sroka
- 39,540
- 23
- 113
- 143
28
votes
2 answers
update specific bit in integer column
I have a mysql table where user permissions are stored as a bitmask:
|user | permissions |
| Admin | 31 |
| User | 13 |
16 8 4 2 1
Admin -> 1 1 1 1 1 -> 16 + 8 + 4 + 2 + 1 -> 31
User -> 0 1 1 0 1 ->…

Jürgen Steinblock
- 30,746
- 24
- 119
- 189
25
votes
3 answers
How do I check, if bitmask contains bit?
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…

Nicolai
- 2,835
- 7
- 42
- 52