bitmasks are data used for bitwise operations.
Questions tagged [bit-masks]
54 questions
44
votes
6 answers
What is the purpose of "int mask = ~0;"?
I saw the following line of code here in C.
int mask = ~0;
I have printed the value of mask in C and C++. It always prints -1.
So I do have some questions:
Why assigning value ~0 to the mask variable?
What is the purpose of ~0?
Can we use -1…

Jayesh
- 4,755
- 9
- 32
- 62
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
8
votes
7 answers
Bitmask switch statement
I have this code in a section of my project:
enum myEnum
{
invalid = -1,
val1 = 1,
val2 = 2,
val3 = 4
};
int bitmask = val1 | val3;
if(bitmask & val1)
...
if(bitmask & val2)
...
if(bitmask & val3)
...
This is fine, and…

Tom
- 908
- 1
- 6
- 14
7
votes
2 answers
Pygame - is there any way to only blit or update in a mask
Is there any way in pygame to blit something to the screen inside a mask. Eg: if you had a mask where all the bits were set to 1 except for the topleft corner and a fully black image, without changing the image, could you keep the top left corner…

user1912132
- 75
- 2
- 8
5
votes
1 answer
How to interpret this Swift SpriteKit example code of a physics body bitmask system
I was taking a deep look into Apples SpriteKit & GameplayKit example code and found a Project called 'DemoBots' written in Swift. There are some very interesting concepts used in that projects which I wanted to adapt into my projects.
I was already…

Simon Kemper
- 635
- 1
- 7
- 16
4
votes
2 answers
Can someone explain how this bitMask code works?
This is code that my partner came up with but for some reason I can't get a hold of him to ask him how it's suppose to work. I've been through it many times now and can't seem to get the answer I'm suppose to get.
/**
* bitMask - Generate a mask…

Ryan Fasching
- 449
- 2
- 11
- 21
3
votes
3 answers
Is there a concise way to create bit masks in C?
I need to create three bit masks that end up in three 32-bit unsigned ints (let's call them x, y and z). The masks should end up like this:
x: 0000 0001 1111 1111 1111 1111 1111 1111
y: 0000 1110 0000 0000 0000 0000 0000 0000
z: 1111 0000 0000 0000…

Lee Netherton
- 21,347
- 12
- 68
- 102
3
votes
1 answer
Swift protocol with member that is of 'ObjectSetType'
I'm currently writing a game in swift and I'm trying to make use of protocols to define things such as Chapters and Levels etc.
So a chapter might have the following structure:
protocol Chapter {
var title:String {get}
var…

TommyBs
- 9,354
- 4
- 34
- 65
3
votes
2 answers
How to Check for existence of Hamiltonian walk in O(2^n) of memory and O(2^n *n) of time
We can simply modify the travelling salesman problem to get whether a Hamilton walk exists or not in O(2^N * N^2)Time Complexity.
But I read it at codeforces that it is possible to solve this problem in O(2^N * N) Time .
Although , I cannot…

Shubham Sharma
- 799
- 7
- 31
3
votes
1 answer
Storing multiple values via bitmask in c#
I'm trying to store four independent 5-bit values (0-31) inside a 32-bit int via bit mask but am having trouble getting the values correct to set and get the individual values from the masked int used for storage.
Can anyone help me with…

CoryG
- 2,429
- 3
- 25
- 60
3
votes
4 answers
Matlab: How to properly get a mask equivalent to 2^63-1?
I'm having some problems with MATLAB and 64-bit integers. I want to have a mask equivalent to 2^63-1 (all ones except the MSB), but MATLAB just seems to round everything.
>> mask_fraction = uint64(9223372036854775807)
mask_fraction =…

Mewa
- 502
- 1
- 9
- 24
3
votes
1 answer
mysql / bitmask: select for NOT value
I have a table with a bitmask column(unsigned int). This field has values 1, 2, 4, 8, 16, etc.
How do I select for NOT a specific value? IE I don't care what the other bits are - only that a specific bit be 0.
What I've tried:
select count(*) from…

ethrbunny
- 10,379
- 9
- 69
- 131
3
votes
2 answers
Implement bitmask or relational ACL in PHP
I think PHP people are familiar with the E_ALL and various other bitmask constants from the error_reporting() function. They are number constants, example: E_ALL means 32676 and E_NOTICE means 8. I can say that I want all errors but notice shown,…

Whisperity
- 3,012
- 1
- 19
- 36
2
votes
4 answers
Bit Mask Question
Readng the documentation for a point of sale system, here is the example they give for a mask that is supposed to tell you what seats are selected. I can't figure this out. I completely understand bit masking. Is this example just…

Bob
- 47
- 4
- 8
2
votes
3 answers
replace mask with original image opencv Python
I am trying to replace objects which I found using a mask with the original images pixels. I have a mask that shows black where the object is not detected and white if detected. I am then using the image in a where statement
image[np.where((image2…

mDumple
- 45
- 2
- 7