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
6
votes
3 answers

What are the rules for bitmasks? Like 0xFF vs. 0xFC

Im working on a game that creates Procedurally generated dungeons, I found an example that uses bit masking to retrieve things like room number and type of door. In the example he uses a bitmask to pull details from the integer for each tile. and…
John Close
  • 63
  • 1
  • 1
  • 3
5
votes
3 answers

Can I create a 1-to-64 bits bitmask without a conditional in Java?

I want to write a function that takes an int between 1 and 64, and returns an appropriate "bitmask", with as many 1 bits as the input. I started like this: /** Computes a bitmaks */ private static long mask(final int bitsPerValue) { return (1L…
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
5
votes
5 answers

Is there an elegant way to store a dual relationship (i.e. user 1 and user 2 are friends)

I've run into the same problem in two different pieces of work this month: Version 1: User 1 & User 2 are friends Version 2: Axis 1 & Axis 2 when graphed should have the quadrants colored... The problem is, I don't see an elegant way, using a…
Ted
  • 12,122
  • 4
  • 31
  • 39
5
votes
1 answer

In C++ use a C bitmask (anonymous enum) exposed by iOS native library

Cocoa utilises typedef-ed anonymous enum bitfields. I'm using objective-C++, for better & worse. Inside a .mm file I need to assign 2 bits (bitwise inclusive OR) to a property of the type of one of these enum bitfield types. The libc++ compiler…
codey
  • 71
  • 3
5
votes
4 answers

Declaring masks for bitwise operations

I'm new to low level operations like this, I'm hoping someone can point out the obvious mistake I must be making here. //Input value - 00111100 //I want to get the value of the bits at indexes 1-3 i.e 0111. byte mask = (byte)0x00001111; // This…
Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75
5
votes
1 answer

Possible to prevent accidental bitwise operators on non-bitmask field?

In C# you are recommended to add the [Flags] attribute to bitmask enumerations, like so: [Flags] public enum Condiments { None = 0, Ketchup = 1, Mustard = 2, Mayo = 4, Pickle = 8, AllTheWay = 15 } I discovered I had code…
Joel P.
  • 869
  • 10
  • 20
5
votes
4 answers

what is the most efficient way to flip all the bits from the least significant bit up to the most significant last 1 bit value?

Say for example I have a uint8_t that can be of any value, and I only want to flip all the bits from the least significant bit up to the most significant last 1 bit value? How would I do that in the most efficient way?, Is there a solution where I…
0xdeadbeef
  • 500
  • 3
  • 17
5
votes
1 answer

Bitmasking and Bitwise Operations in Golang

I'm a beginner to programming in general so i'm sorry if i make some mistakes while putting this question up. The tutorial I'm following goes over this code: package main import ( "fmt" ) const ( isAdmin = 1 << iota isHeadquarters …
Sinoreth
  • 61
  • 1
  • 4
5
votes
1 answer

How to achieve the effect of vpmovmskb on ZMM registers?

The quirky instruction (v)pmovmskb, dating back to SSE, takes the most significant bits of the bytes in an mm, xmm, or ymm register and moves them into a general purpose register. This is very useful for classifying vector elements or performing…
fuz
  • 88,405
  • 25
  • 200
  • 352
5
votes
2 answers

Bitfields VS. Bitmasks

I've had some experience in C before, however I have never seen the bitfields feature before. I know one can use bitmasks to isolate certain bits in a data structure, but why then bother with bitfields? For example, say the bits we want to isolate…
nanoman
  • 341
  • 4
  • 11
5
votes
4 answers

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

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 application, you know this kind of thing: 1 - Apple 2 - Orange 4 - Banana 8 - Plum etc. In this case the number 11 would…
Lunatik
  • 3,838
  • 6
  • 37
  • 52
5
votes
1 answer

How to wrap a function returning a bitmask in Python

Consider the C function int get_events(), which returns a bitmask of the following events: #define EVENT_KEY_DOWN 1 #define EVENT_KEY_UP 2 #define EVENT_KEY_PRESS 4 It could return 5, for example, meaning both EVENT_KEY_DOWN and…
Rômulo Ceccon
  • 10,081
  • 5
  • 39
  • 47
5
votes
2 answers

Building a bit flag using linq / lambda

Is it possible to build up a bit mask based on the result of a linq query; for example: class MyClass { public int Flag{get;set;} public bool IsSelected {get;set;} } myVar = GetlistMyClass(); int myFlag = myVar.Where(a =>…
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
5
votes
1 answer

How to check bitfields (SCNetworkReachabilityFlags in particular) for flags in Swift?

I have a SCNetworkReachabilityFlags variable and want to check it for particular values, e.g. if the network is reachable via WWAN. The SCNetworkReachabilityFlags type is a typealias for UInt32 and the various options are defined as Int…
snod
  • 2,442
  • 3
  • 20
  • 22
5
votes
3 answers

Lowercase<-->Uppercase Function Not Working As Planned

So, what I'm trying to do is create a function that switches uppercase characters to lowercase and vice-versa. Here is what I'm working with: #include #include int caplowswitch(char string[], char switched[]); int main(){ …
Brandacus
  • 419
  • 3
  • 12