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
14
votes
5 answers

Improve this PHP bitfield class for settings/permissions?

I have been trying to figure out the best way to use bitmask or bitfields in PHP for a long time now for different areas of my application for different user settings and permissions. The farthest I have come so far is from a class contributed by…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
14
votes
5 answers

Convert a 64 bit integer into 8 separate 1 byte integers in python

In python, I have been given a 64 bit integer. This Integer was created by taking several different 8 bit integers and mashing them together into one giant 64 bit integer. It is my job to separate them again. For example: Source number:…
JHixson
  • 1,512
  • 2
  • 14
  • 29
13
votes
2 answers

Calculate the number of unordered pairs in an array whose bitwise "AND" is a power of 2 in O(n) or O(n*log(n))

How to calculate number of unordered pairs in an array whose bitwise AND is a power of 2. For ex if the array is [10,7,2,8,3]. The answer is 6. Explanation(0-based index): a[0]&a[1] = 2 a[0]&a[2] = 2 a[0]&a[3] = 8 a[0]&a[4] = 2 a[1]&a[2] =…
13
votes
5 answers

Java Working with bits

Let me start by saying I have never really worked with bits before in programming. I have an object that can be in 3 states and I want to represent those states using a 3 bit array. For example: I have a race car and it can go forward,left, and…
Tegra Detra
  • 24,551
  • 17
  • 53
  • 78
13
votes
16 answers

Can I allocate a specific number of bits in C?

I am trying to store a large amount of boolean information that is determined at run-time. I was wondering what the best method might be. I have currently been trying to allocate the memory using: pStatus = malloc((/8) + 1);…
Tim
  • 1,814
  • 8
  • 29
  • 41
12
votes
4 answers

SQL Server: varbinary or int to store a bit mask?

Is there any advantage of using int vs varbinary for storing bit masks in terms of performance or flexibility. For my purposes, I will always be doing reads on these bit masks (no writes or updates).
aleemb
  • 31,265
  • 19
  • 98
  • 114
12
votes
2 answers

Bitwise subtraction

Given the enum: [Flags] enum foo { a = 1, b = 2, c = 4 } then foo example = a | b; If I don't know if foo contains c, previously I have been writing the following if (example & foo.c == foo.c) example = example ^ foo.c; Is there a way to do…
maxp
  • 24,209
  • 39
  • 123
  • 201
12
votes
2 answers

Bit mask in Bash

Is something like the following code possible within a shell script? var1=0xA (0b1010) if ( (var1 & 0x3) == 0x2 ){ ...perform action... } Just to make my intentions 100% clear my desired action would be to check bits of var1 at 0x3 (0b0011)…
Jens Petersen
  • 189
  • 2
  • 11
12
votes
2 answers

Simplify (A & B) && !(A & C)

A, B, and C are variables of some unsigned integral type. Conceptually, A is a test vector, B is a bitmask of 'required' bits (at least one corresponding bit in A must be set) and C is a bitmask of 'prohibited' bits (no corresponding bit in A may…
Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
11
votes
1 answer

How to create mask in a machine independent way?

So I'm practicing some programming interview questions, and stumbled across this sample pdf which recommends "Understand how to use masks and create them in an machine independent way". But it doesn't eloborate the difference between machine…
11
votes
3 answers

Game Engine Collison Bitmask... Why 0x01 etc?

Coming across this situation both in Sprite Kit (iOS Development) and in Cocos2d-x (which I know was pretty much the inspiration for Sprite Kit, hence why they use a lot of the same tools), I finally decided to figure out why this happens: When…
Kjell
  • 801
  • 7
  • 19
11
votes
2 answers

Fastest way to convert a integer to arbitrarily ordered byte arrays in JavaScript?

I'm looking to convert the MIN_SAFE_INTEGER through MAX_SAFE_INTEGER range of a JavaScript number (53-bits not including the sign) into a string of bits spread over 7 bytes shifted two to allow for sign and null identifiers. Thus far the best I've…
CoryG
  • 2,429
  • 3
  • 25
  • 60
11
votes
4 answers

Using SQL to determine cidr value of a subnet mask

I'd like to find a way to do a SQL query that will calculate the cidr (bit representation) of a subnet mask stored in the database. So for example, I've got either 255.255.255.0 or its decimal value (4294967040) stored in the database. I'd like to…
Matt P
  • 5,447
  • 4
  • 23
  • 20
11
votes
1 answer

Fastest way to unpack 32 bits to a 32 byte SIMD vector

Having 32 bits stored in a uint32_t in memory, what's the fastest way to unpack each bit to a separate byte element of an AVX register? The bits can be in any position within their respective byte. Edit: to clarify, I mean bit 0 goes to byte 0, bit…
alecco
  • 2,914
  • 1
  • 28
  • 37
11
votes
2 answers

Java get bit ranges from a long

I need to extract certain bit ranges from with a long value, for example: long input = 15367 (11110000000111) What I need to then do is to extract two long values from the original long, First long is 5 bits starting from bit 0, so bits 0:4 = 7…
Tony
  • 3,587
  • 8
  • 44
  • 77
1 2
3
49 50