Questions tagged [bitmask]

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.

738 questions
10
votes
6 answers

Regarding bit masking in C. Why (~(~0 << N)) is preferred than ((1 << N) -1)?

I do know that ~0 will evaluate the maximum word sized bit 1s (and thus takes caring of portability), but I am still not getting why ((1 << N) - 1) is discouraged? Please share if you used the second form and got into any trouble.
MS.
  • 881
  • 2
  • 9
  • 23
10
votes
4 answers

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…
Kit
  • 30,365
  • 39
  • 105
  • 149
10
votes
4 answers

How to efficiently check against bitmask?

I am using inotify and want to efficiently check against the reported bitmask event (see inotify man page). Now I could brutely check against every bit on every event, but that would be extremely crude, if not stupid, as I would have N conditionals…
alex
  • 1,277
  • 3
  • 14
  • 30
9
votes
4 answers

Combining Enum Value using Bitmask

I understand it's possible to use bitmasks in enum values, but I don't know how to create it. I have a simple enum : enum State { minimizing = 0, maximizing, minimized, maximized }; A state is always State.minimized or…
Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88
9
votes
5 answers

Fastest way to produce a mask with n ones starting at position i

What is the fastest way (in terms of cpu cycles on common modern architecture), to produce a mask with len bits set to 1 starting at position pos: template constexpr T make_mask(std::size_t pos, std::size_t len) { // Body of the…
Vincent
  • 57,703
  • 61
  • 205
  • 388
9
votes
1 answer

NumPy boolean array warning?

I have a few numpy arrays, lets say a, b, and c, and have created a mask to apply to all of them. I am trying to mask them as such: a = a[mask] where mask is a bool array. It is worth noting that I have verified that len(a) = len(b) = len(c) =…
pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
9
votes
5 answers

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…
Nathan
  • 2,941
  • 6
  • 49
  • 80
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
8
votes
6 answers

Creating a mask with N least significant bits set

I would like to create a macro or function1 mask(n) which given a number n returns an unsigned integer with its n least significant bits set. Although this seems like it should be a basic primitive with heavily discussed implementations which…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
8
votes
1 answer

Bit masking (javascript): how to check ALL flags

If I have the number '00001000' and the mask '00101000', how can i check, with a binary operation, if both the bits in the number are set? number & mask return true if at least one bit match, but I need to check for all of them to match. How to?
pistacchio
  • 56,889
  • 107
  • 278
  • 420
8
votes
7 answers

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…
user297459
8
votes
4 answers

Matching binary patterns in C

I'm currently developing a C program that needs to parse some bespoke data structures, fortunately I know how they are structured, however I'm not sure of how to implement my parser in C. Each of the structures are 32-bits in length, and each…
Tony
  • 3,587
  • 8
  • 44
  • 77
8
votes
2 answers

Correct usage of bitmask?

heyhey, just have a question about bitmasks. I think I know now what they are and where they can be used. I want to store specific permissions like BUILD, BREAK and INTERACT and maybe more for specific groups. The code below should do this but Im…
maxammann
  • 1,018
  • 3
  • 11
  • 17
8
votes
1 answer

Using sigaction(), c

I was doing a little reading about sigaction() (sources are from my course notes) and I'm not sure I understand this text: The signal mask is calculated and installed only for the duration of the signal handler. By default, the signal “sig” is…
yotamoo
  • 5,334
  • 13
  • 49
  • 61
7
votes
3 answers

C++11 and [17.5.2.1.3] Bitmask Types

The Standard allows one to choose between an integer type, an enum, and a std::bitset. Why would a library implementor use one over the other given these choices? Case in point, llvm's libcxx appears to use a combination of (at least) two of these…
user1290696
  • 489
  • 1
  • 4
  • 10