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.
771
questions
0
votes
0
answers
86
views
vector.puch_back() improving performance
is there any way to improve the performance of this function? As I have to call it ~10-100M times for 1 calculation it's slowing down my program and most time has been lost in this line:
myHand_param....
0
votes
2
answers
95
views
Do I have to set most significant bits to zero if I shift right?
Let's say I have a 64-bit number and some bits that are set that hold a value, let's say three bits. I have a mask to get that value. To get the value of those three bits I bitwise 'AND' the number ...
0
votes
2
answers
30
views
Using Bitmask to read first 2 bits of a ushort in C#
I have an array of ushorts and want to iterate over the array and check if the first two bits of that ushort are 11 if so I want to clear them. However the bit mask I am using to read the first two ...
0
votes
1
answer
47
views
Candump (SocketCAN) ID range filtering feature
I need to filter ranges of IDs ([0x5B to 0x7B] + [0x87 to 0xCC]) and i'm confused with bitmasking to do it:
For the first range, i tried :
candump vcan0,5B:7DF
which let throw only 0x5B and 0x7B ...
0
votes
2
answers
128
views
Detecting error in Number of Solutions in N-Queens Problem
Programming Problem Description
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an
integer n, return the number of ...
0
votes
2
answers
55
views
Converting from decimal to base 4 using bitmasking
I'm trying to write a program that converts from decimal to base 4 using bit masking. First I wrote this program that converts from decimal to binary to understand how it works
#include <stdio.h>...
1
vote
1
answer
72
views
Why does VPMOVMSKB appear to produce incorrect results?
According to the Intel documentation, vpmovmskb does:
Instruction: vpmovmskb r32, ymm
Create mask from the most significant bit of each 8-bit element in a, and store the result in dst.
According to ...
-1
votes
1
answer
41
views
How to convert bit to bit shift value
I have a difficulty selector set as an enum (None=0, Easy = 1<<0, Medium = 1<<1, Hard = 1<<2, Expert = 1<<3). Along with this, I have an array of point values I want to assign ...
0
votes
1
answer
42
views
Determining 32-bit Mask in C
I'm not a C programmer, but found myself in a situation where I need to do some coding. I am using an ADC to determine voltage levels running through a device, and the ADC has 3 channels (0, 1, 2). I ...
1
vote
0
answers
28
views
GLSL big-endian/little-endian compatibility
In my pixel format, I have the following union (c++ end):
union
{
int mInteger;
struct
{
short mData1;
short mData2;
};
};
in GLSL I want to access whatever's in ...
0
votes
3
answers
104
views
How to set enum val of fixed size to its max possible value?
This is probably simple but I'm not getting it right. I have a "bitmask" enum which has a value all that indicates that all bits are set. However, I can't get it to flip all bits using ~0. ...
5
votes
4
answers
253
views
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 ...
0
votes
0
answers
24
views
pointers and casting not correct for my method to update LSB in array in C
I'm trying to update an array that is 16 bits (6 MSB are item id and 10 LSB are spaces) and I'm drowning in errors. It's used for inventory and the layout of the pattern is aisles (10 total), sections(...
1
vote
2
answers
76
views
Making bitmasks in C
I'm new to bit manipulation but I'm struggling to translate it to code.
For the following function I need to make a bitmask
void make_bitmask(unsigned int width, unsigned int shift,unsigned int* mask)
...
0
votes
1
answer
138
views
bitwise operations with c++11 enums
I have this c++11 class which contains a variety of enums. These enums contain bitmasks which eventual all get combined together into an 8-bit values using bitwise operators.
It looks like this:
class ...
1
vote
1
answer
32
views
How to write an extension that calculates bitmask from a given list of enums
I have an enum
public enum MyEnum{
None =0,
First = 1,
Second = 2,
Thirds = 4
}
I have a list of these enums
List<MyEnum> MyEnumList;
I would like to get a bitmask result of MyEnum ...
0
votes
1
answer
85
views
Bit-masking - C++ to Python
I have come across a C++ function in my work that extracts integer numbers from a 16bit unsigned integer using bit-masking.
I need to port this over to Python, and am unsure if the same syntax is ...
0
votes
1
answer
61
views
Checking integer tuples for binary conditions efficiently
I am writing a piece of code which takes 5 lists of integers (ships), a bitmask (hits) and outputs the number of 5-tuples (one integer from each list) that are consistent with the following two ...
0
votes
2
answers
97
views
Convert power of 2 bitmask into corresponding index [duplicate]
I've been looking around for an answer to this, but haven't found any decent solutions. I'm trying to convert a single-enabled-bit (32-bit mask) into its corresponding index location. The mask has ...
1
vote
1
answer
124
views
Bitwise operators in JavaScript as flags on a state
I am trying to install some logic into a piece of JS I am working on that will simply add/remove flags depending on some conditions.
It primarily consists of two steps
Build object based on some ...
0
votes
0
answers
39
views
a bit is set unexpectedly in bitmasking
I am trying to implement set which can only store values upto 60 through bitmasking (constant space and time).
But whenever i try to set a particular bit another bit also sets.
Here, x.print() is ...
3
votes
2
answers
106
views
C# - Construct a signal Vector<T> from an integer bitmask
I have some integer value representing a bitmask, for example 154 = 0b10011010, and I want to construct a corresponding signal Vector<T> instance <0, -1, 0, -1, -1, 0, 0, -1>.
Surely there ...
-2
votes
1
answer
104
views
Convert decimal string IP mask to unsigned int with trailing zeros
I get a mask as a string that I retrieve using strtol and strtok and that I want to save in an unsigned int x, trail with zeros, and & it with an IP represented also as unsigned int in order to ...
1
vote
1
answer
161
views
Split multiple bytes according to bit masks
I'm trying to split bytes according to bit masks.
Consider these two bytes (1000 in binary as short int):
11101000
00000011
and these three masks:
00000011
11111111
00111111
What I want to do is to ...
1
vote
0
answers
56
views
How to write a BSR (Bit Scan Reverse) in generic cross-platform ANSI C? [duplicate]
I need to find out how to do a Bit Scan Reverse (return the index of the highest set bit) for 8-bit, 16-bit, 32-bit and 64-bit integers in platform-independent ANSI C for use in an industrial process.
...
0
votes
0
answers
37
views
base64ArrayBuffer : why to use a mask to extract the first part of the chunk?
On the below code, could we just write :
a = chunk >> 18 // 16515072 = (2^6 - 1) << 18
instead of :
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
I don't see ...
0
votes
0
answers
14
views
Language agnostic logic issue regarding Tile bit masking
I am creating a bit masking framework (specifically for generating flooring textures), and for the algorithm to work a bit mask object requires 48 individual textures (1 texture if the cell is false, ...
2
votes
1
answer
141
views
How does this bitwise hamming(31,26) encoder work in C? (Bitmasking)
I understand what the individual operations are (such as the bitwise ANDs an ORs), but I don't understand why they have been used where they have been.
Also, my understanding is that the first set of ...
0
votes
0
answers
57
views
.NET bitarray in SQL Server and using masks to find answers
I have data in .NET where the bitarray is used as a key. The bitarray can be variable length but probably no more than 500 bits. I then want to take another bitarray and find all items that fully ...
0
votes
2
answers
79
views
C set 3 bits for a particular number
I am trying to understand masking concept and want to set bits 24,25,26 of a uint32_t number in C.
example i have
uint32_t data =0;
I am taking an input from user of uint_8 which can be only be value ...
2
votes
2
answers
115
views
How to avoid bitwise operations outside the width of the data type in c++
For sake of experiment lets have a function that takes in a bitmask and offset and returns the mask shifted by offset. What would be a performance friendly way to determine if the operation will not ...
2
votes
1
answer
72
views
Can you extract bitmask directly from bitfield in C++?
Considering following example:
#include <iostream>
using namespace std;
struct Test
{
uint8_t A:1;
uint8_t B:1;
uint8_t C:1;
uint8_t D:1;
};
int main()
{
Test test;
...
1
vote
1
answer
151
views
How to set masked bits to a specified number?
I haven't been able to find an answer to this on Google, nor do I have any better search ideas. If I have a 2 byte number, a mask, and a third number, how do I replace the masked bits with the third ...
0
votes
2
answers
198
views
Finding the location of ones in a bit mask - Julia
I have a series of values that are each being stored as UInt16. Each of these numbers represents a bitmask - these numbers are commands that have been sent to a microprocessor telling it which pins ...
0
votes
1
answer
33
views
Check which flags are included inside a decimal
I need to check which hexadecimal flags are included in a certain decimal variable. At the moment the script just works for small decimals but I've some longer ones.
In the following example it shows ...
2
votes
1
answer
1k
views
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"
...
-2
votes
1
answer
54
views
Intertwine 0s betweeen bits SystemVerilog [closed]
I want to intertwine 0s between the bits of a variable. For example, imagine I have a variable a as follows
a = 1101
Then, I want to obtain the following result
b = 1_000000_1_000000_0_000000_1
That ...
0
votes
0
answers
51
views
Alternarive solution for bitmask enum values
All
Due to the bitmask enum value limits (Maximum items "Version" and "Hardware" can hold is 32),
I want change the data structure of "Hardwares". In current code, the &...
-2
votes
1
answer
44
views
Why ip is not part of 115.64.4.0/22 [closed]
For 115.64.4.0/22 why only b and d are correct
I know it means the network has assigned the first 22 bits are assigned for host.. Can somoene explain me why only those 2 are part of it.
Please help ...
0
votes
0
answers
109
views
SQL Server columns_updated() function - understand the result
I want to use the T-SQL columns_updated() function in my triggers to find out which cols were updated.
The thing is when reading the function output its:
0x0002000000000000000000
which indicated that ...
0
votes
1
answer
90
views
Return the count of all prime numbers in range [a,b] such that all the digits are from set {1,5,9} . 1<=a<=b<=10⁹
Return the count of all prime numbers in range [a,b] such that all the digits are from set {1,5,9} . 1<=a<=b<=10⁹.
My approach -
I was trying to generate all the numbers which are from set {1,...
2
votes
0
answers
167
views
finding solution for Bin packing problem using bitmasking under smaller constraints
We have n box and an array of weights, where weight of ith box is given by arr[i]. We have a container which can carry a maximum weight of w. Now we have to find minimum number of containers required ...
0
votes
1
answer
61
views
How this code block avoiding hard-coding. Elements Of Programming Interviews Question 5.1
I recently started preparing for job interviews and came across with this example question which ask us to find how many bits are set to 1 in a number and answer code block.
The Following program ...
0
votes
1
answer
71
views
Encode an array of long values as a BitSet
I saw that we can encode an array of ints into a BitSet and retrieve them as in :
int[] ints = new int[]{5, 7, 25, 100102244};
BitSet b = new BitSet();
for (int i=0;i<ints.length;i++)
b.set(...
0
votes
1
answer
103
views
PLSQL bitmask values
I am in this situation, I cannot validate the bit for the print permission. Unfortunately I can't have a bitmask with a single bit lit. Can you give me some suggestions?
SELECT
DECODE(BITAND(...
-1
votes
1
answer
149
views
Comparing a negative and positive integer's byte array values [duplicate]
I am pretty much new to C++ and especially signed and unsigned conversions. Currently I am doing one exercise, where I need to compare a value if it is between a two values (one minimum and maximum).
...
1
vote
1
answer
90
views
Write expressions that use bitwise operators, equality checks, and Boolean operators to do the following in O(1) time
• Right propagate the rightmost set bit in x, e.g., turns (01010000) to (01011111).
I am stuck on this one. I can mask out the right-most bit with x &= ~(x-1) how can I set the bits after it as ...
2
votes
4
answers
222
views
Bitmask for exactly one byte in C
My goal is to save a long in four bytes like this:
unsigned char bytes[4];
unsigned long n = 123;
bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> ...
0
votes
0
answers
83
views
Making a constexpr binary mask out of enum values and with a variadic template
I am trying to make a class for generating constexpr bitmasks out of enum values.
However, I get several compilation errors with the code below, especially:
makeMask(args...) has "expected ')' &...
2
votes
3
answers
157
views
A bitmask solution to test if an element is included in a finite set?
This is a code challenge. I came across this question on SO about testing if a number is included in a given set. The typical solution would be:
const givenSet = [9, 91, 20, 18, 17, 37]
function ...