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
2 answers
why do bit operations not work correctly
I'm at a complete loss! Why does my code not work as expected?
I have the following code:
UINT64 tmpA = 0;
UINT64 tmpB = 0;
UINT64 alarmed_lans = 0;
int foprtmsk[2]={0};
switch_fo_prtmsk_getptr(foprtmsk);
tmpA = foprtmsk[1]; …

stdcerr
- 13,725
- 25
- 71
- 128
-1
votes
1 answer
Binary numbers, how I can use bitmasks to certain bits?
I have this example:
If we take the binary representation of the decimal value 220 (1101 1100) and we wanted to extract the higher 4 bits, we could use a bitmask with the boolean AND operation:
1101 1100 (220)
AND 1111 0000 (240)
…

mohammedtair
- 3
- 3
-1
votes
1 answer
How do I find the complement of 0x87654321 using bitwise C operations?
I am learning about masking techniques in C.
Here is a practice problem I am working on:
Need to find the complement of 0x87654321 while leaving the least significant byte intact which should look like this 0x789ABC21
The only mask I'm familiar…

ixbo45
- 313
- 1
- 2
- 7
-1
votes
2 answers
What does mask variable do in this CRC checksum calculation?
The question is about code in figure 14-6 in here.
The mask is calculated as:
mask = -(crc & 1)
Why do we & crc with 1 and then make result negative? The Figure 14-5 does not have this mask variable, why?
Edit:
So since this point is clear, why do…

quantum231
- 2,420
- 3
- 31
- 53
-1
votes
1 answer
K&R C Invert Function
Exercise 2-7. Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
#include
int invert(unsigned int x, int p, int n)
{
…

Tommy Saechao
- 1,099
- 4
- 17
- 28
-1
votes
1 answer
Shift left with variable error
I'm trying to create a mask, but when I calculate the value of shift and put it inside a variable I get the wrong result. Look:
#include
int main(){
int shift = 32 ;
printf("%d\n",shift); //32
int mask =…

Davisson Paulino
- 45
- 6
-1
votes
2 answers
How use bitmask operator
I'm learning bit mask. And found and example but couldn't make it work.
I'm trying to calculate all sum combination from one array.
The result should be
0 - 1 - 2 - 3 - 3 - 4 - 5 - 6
My problem is with (i & mask) should only result in {0,1} and…

Juan Carlos Oropeza
- 47,252
- 12
- 78
- 118
-1
votes
2 answers
bitmasking and binary arithmetic
I'd like to know the science behind the following. a 32 bit value is shifted left 32 times in a 64 bit type, then a division is performed. somehow the precision is contained within the last 32 bits and in order to retrieve the value as a floating…

cool mr croc
- 725
- 1
- 13
- 33
-1
votes
2 answers
Splitting a 200 bit hexadecimal bitmask
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different…

ESD
- 675
- 2
- 12
- 35
-1
votes
2 answers
Set the rest of the bits to 1 in Java
I need some help with bitwise operations.
I have number(64 bit) were first 16 bits are meaningful, and I'd like to set rest of them to "1"
00000000 11000001 00000000 00000000 ... <- currrent value
00000000 11000001 11111111 11111111 ... <- result…

Constantine
- 1,802
- 3
- 23
- 37
-1
votes
1 answer
Bit Masking in a Cache
In C I am attempting to separate an integer address value into the tag bit and the set index bit in a direct mapped cache with 4 sets. I do this so I can compare the correct tags at the line in the correct set of my cache. Here for example the…

Branbron
- 101
- 4
- 12
-1
votes
1 answer
Bit manipulation in C - builtin function
Is there any builtin function in C that returns me the first bit equals to zero in a 32-bit integer?
I know I can check all 32-bit using a for-loop:
value <--- parameter (uint32_t)
for (int i=0; i<32; i++){
uint32_t pos = 1 << i;
if (pos ^…

Guilherme
- 443
- 5
- 22
-1
votes
1 answer
applying a bit vector for mask
I want to do a simple bit mask operation. Lets say
uint64_t a = 348659235483;
Assuming this number is converted to binary, I want to extract the values from bit 6 to 12 (0 is MSB on the right end). What is the smallest code for that?
the binary…

mahmood
- 23,197
- 49
- 147
- 242
-2
votes
1 answer
Bit masking. Why do we need bitwise shift operator?
I have seen two kinds of bit masking implementations:
The one that uses bitwise shift operator to set flags |= 1 << MASK_VALUE and clear the mask flags &= ~(1 << MASK_VALUE). This is the approach that is used most frequently.
The other one doesn't…

FAMO4S
- 17
- 4
-2
votes
1 answer
Why the following xor operation is resulting the wrong answer?
#include
#include
using namespace std;
int main()
{
vector input = {2,3,4,5,4,3,7,2};
int XOR = 0;
for (auto& i : input)
{
XOR = XOR ^ i;
}
return XOR;
}
This returns 2, when it should return…

Nick Kamal
- 15
- 2