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.
Questions tagged [bitmask]
738 questions
-1
votes
1 answer
Maximum Sum of XOR operation on a selected element with array elements with an optimize approach
Problem: Choose an element from the array to maximize the sum after XOR all elements in the array.
Input for problem statement:
N=3
A=[15,11,8]
Output:
11
Approach:
(15^15)+(15^11)+(15^8)=11
My Code for brute force approach:
def compute(N,A):
…

Samyak Jain
- 1
- 1
-1
votes
1 answer
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 to these difficulties. So the array has indexes as so. [0, 100, 133, 166,…

Chris McCole
- 59
- 2
- 15
-1
votes
1 answer
Comparing a negative and positive integer's byte array values
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).
For example
minimum value = -319 - it's byte…

susil95
- 300
- 3
- 5
- 16
-1
votes
1 answer
Count integers in an array where the set bits are a subset of a given mask
Given a mask and a value, the mask covers the value if all bits from the value fall into the mask.
For example:
mask: 0b011010
value: 0b010010
true
or
mask: 0b011010
value: 0b010110
false
For int arr[arr_size], I need to calculate how many…

Zhihar
- 1,306
- 1
- 22
- 45
-1
votes
1 answer
Why doesn't my bitmask program work? And how can i reset / set a bit and then print the number
int main() {
unsigned int nr, mask;
printf("nr = "); //for eg. if i use number 11111111 or 00111111 it prints 0.
scanf("%hu", &nr);
if ((nr & 0b00001000) == 0)
printf("0\n\n");
else
printf("1\n\n");
mask =…

Bufni
- 1
-1
votes
1 answer
Is there a better way to mask bits?
So I am trying to split memory addresses into their page number and offset components by masking the hex numbers. But to cover a decent variety of addresses of different bits and page sizes, I seem to have to write a lot of nested if else…

Casper
- 29
- 6
-1
votes
1 answer
What does 1 << i means and how can I use it for?
I saw someone used 1 << i, sometimes 1 << n. What is this and how can I use it for?

An Nguyen
- 13
- 2
-1
votes
2 answers
Which is the best way to set a bit & why?
If I had to set 4th bit,
Which is the best way
Method 1:
a |= (1 << 3)
Method 2:
a |= 8
Please elaborate the difference.

SRIKANTH
- 65
- 7
-1
votes
1 answer
How to check the bitmask of result of an xor operation of 2 (or more) values?
I have two bit masks:
public static UInt64 Mask0 = 0xFFFFFFFF00000000UL;
public static UInt64 Mask1 = 0x00000000FFFFFFFFUL;
I have a ulong value:
UInt64 value = 0x1089000000054321;
Then based on a condition, I xor the value with one of the two bit…

Jain
- 1
- 2
-1
votes
1 answer
What does the first usable subnet mean?
I am currently learning about subnetting and my tutor has asked a question:
Base address: 192.168.0.0, mask: 255.255.255.0
Steal 4 bits
What is the last address of the first usable subnet?
I know how to find the first, last, broadcast address but…

Gijs de Blauw
- 13
- 3
-1
votes
1 answer
Bit manipulation (iterate through all subsets)
I don't understand why below doesn't work.
I've tried everything I can do - but no progress.
int b = 0;
int x = (1<<1)|(1<<2)|(1<<3)
do {
// process subset
} while((b=(b-x)&x));
What I wonder is that expression b=(b-x)&x
iterate all subsets.
I…

fian Elf
- 13
- 4
-1
votes
4 answers
Grabbing 'n' binary bits from end of unsigned int in C? Bit masking?
I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.
Say I want to grab the last 8 bits of an unsigned int value, containing…

RadiantBytes
- 9
- 4
-1
votes
2 answers
Bitmasking and bit manipulations with C
I've been trying to understand how bit operations work with C and pointers but no luck. Here is my problem. I have 8 bytes in memory and need to extract the first 61 bits and print. For example: 0000 0000..... 0010 0111 , I need to extract the…

tylerC
- 11
- 2
-1
votes
1 answer
Cannot understand the logic of editorial
Question link : https://www.codechef.com/problems/STR
question is :
Little John just had his first class in school. He was taught first 20
letters of English alphabet and was asked to make words from these
alphabets.
Since he doesn't know many…

Varun Kamani
- 86
- 1
- 8
-1
votes
1 answer
Selecting Specific range bits from a bitset and displaying it
Given a particular bitset<32>, how do I select and display the m LSB bits of the bitset?
m is an integer value.
for example I have 10110111011110111101111011000100 and m = 8, the code should display 11000100.
Thanks

Pavan Swarup
- 21
- 2