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
-2
votes
1 answer
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 keep the MSBs only (At the end I'm going to compare…

RedYoel
- 302
- 2
- 16
-2
votes
1 answer
Intertwine 0s betweeen bits SystemVerilog
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 results from intertwining 6 0s betwen each bit…

shaymin shaymin
- 159
- 1
- 1
- 12
-2
votes
1 answer
Why ip is not part of 115.64.4.0/22
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 me.
a. 115.64.8.32
b 115.64.6.255
c …
user16321566
-2
votes
1 answer
What is the c# equivalent of this c++ code?
unsigned char setBit(unsigned char ch, int n)
{
unsigned char mask = 1 << n;
return ch | mask;
}
I want to change n-bit with 1 in c# but I can't do it. I have only c++ examples.

Meppo
- 23
- 3
-2
votes
1 answer
What is the difference between "arr[i] ^= 1" and "arr[i] ^1" about XOR?
When I write like below
int [] test = {4};
int a = test[0]^=1;
int b = test[0]^1;
I can get this output.
OUTPUT
test[0]^=1 : 5 a : 101
test[0]^1 : 4 b : 100
I think that test[0] = 100 -> test[0]^1 = 101 but it is not.
100
XOR …

Mj choi
- 69
- 1
- 9
-2
votes
1 answer
Convert string of numbers into 'binary representation'
Im recently made the "Winning Lottery Ticket" coding challange on hackerrank.
https://www.hackerrank.com/challenges/winning-lottery-ticket/
The idea is to count the combinations of two lines which contain all numbers from 0-9, in the example below…

filikos
- 1
-2
votes
3 answers
Read a single bit from a buffer of char
I would to implement a function like this:
int read_single_bit(unsigned char* buffer, unsigned int index)
where index is the offset of the bit that I would want to read.
How do I use bit shifting or masking to achieve this?

PenguinEngineer
- 295
- 1
- 8
- 30
-2
votes
2 answers
define SOMETHING ('w' << 8)
I came accros this line of code:
#define BWAKUP ('w' << 8)
What does it do? Its the same as:
#define BWAKUP (167000)
In addition, another definition as :
#define CWAKUP (1 + BWAKUP)
is equivalent to :…

ogs
- 1,139
- 8
- 19
- 42
-2
votes
1 answer
How to know if a decimal number is 0x1000 or 0x0100
I have set of data, with a column value named Type, I need to know if the value is 0x1000 or 0x0010 or 0x0200, I don't have any idea, can you please help

Stranger B.
- 9,004
- 21
- 71
- 108
-2
votes
1 answer
javascript bitmask add from string
I have created the following:
var some_string = 'cb';
var a = 1;
var b = 2;
var c = 4;
var d = 8;
var mask = 0;
I want to store the a+b+c+d vars in the mask which works when I do:
mask |= c; // mask now equals 4
The problem I have is that I need…

letslogic.com
- 27
- 7
-3
votes
0 answers
Why those bitwise operators fail?
According to this Bit masking (javascript): how to check ALL flags
I tried this js but I failed. Do I miss something?
var port = 17;
// this fails.
function checkPort() {
if ((0x1 & port) == 0x1) console.log("1");
if ((0x10 & port) == 0x10)…

krg
- 1
- 3
-3
votes
1 answer
Bit Manipulation and XOR
I have an Question regarding bit manipulation in Algorithms.
Given an array of integers, every element in array appears thrice except for one element which occurs only once. Find that element which appear only once.
Example:
Input : [1, 2, 4, 3,…

Vineet Jain
- 1,515
- 4
- 21
- 31
-3
votes
2 answers
Is bit masking comparable to "accessing an array" in bits?
For all the definitions I've seen of bit masking, they all just dive right into how to bit mask, use bitwise, etc. without explaining a use case for any of it. Is the purpose of updating all the bits you want to keep and all the bits you want to…

user7120923
- 45
- 7
-3
votes
2 answers
How does subsets of subsets iteration work?
I read
for ( x = y; x > 0; x = ( y & (x-1) ) )
generates all subsets of bitmask y.
How does this iteration work? Any intuitive explanation?
source: http://codeforces.com/blog/entry/45223
see suboptimal solution section.

ashish singh
- 6,526
- 2
- 15
- 35
-3
votes
1 answer
c++ bitmask: explanation of a code block
I am new in c++ and i have some difficulties to understand bitmask. If someone could describe in the simplest way how the following code work (http://rosettacode.org/wiki/Combinations#C.2B.2B):
#include
#include
#include…

skualito
- 87
- 7