Questions tagged [bit-manipulation]
The manipulation of individual bits. Operators used may include bitwise AND, OR, XOR, NOT, left-shift, and right-shift.
bit-manipulation
7,969
questions
1
vote
1
answer
30
views
find the bit position(s) which are exactly set twice over multiple bit fields
I've 9 bit fields every bit field has 9 nine bits, the 9 LSBs of an int.
I want to find the bit position(s) which are exactly set twice over all bit fields.
e.g:
0.1111.1111
0.0000.1101
0....
0
votes
0
answers
22
views
Optimizing Code with Bitwise Operators : How Does That Really Work? [closed]
How do bitwise operators (like AND, OR, XOR, left shift, and right shift) manipulate binary data at the bit level, and what are some practical examples of their usage in optimizing code performance or ...
1
vote
1
answer
50
views
How can I generalize some bit twiddling operations to all "unsigned" primitive types?
I'm trying to learn something about number traits and want to write some flexible functions for bit-twiddling. This problem fell out of it:
I have defined a few functions that take arguments width and ...
2
votes
1
answer
32
views
CUDA: Nth set bit indexes using all threads in a warp in O(1) time
I have a 32-bit bit mask holding a set of valid items.
From that bit mask I want to extract the indices of valid entries as a list.
Let's say I obtained the bit mask using a ballot, and I want to know ...
12
votes
1
answer
258
views
C++ Conversion Error when Accessing Bits to Set a Bit Field
We are using bitfields to represent elements of a register read from a device.
#include <cstdint>
struct Register {
uint8_t field : 1;
};
int main() {
uint8_t byte{0}; // Read from a ...
1
vote
2
answers
89
views
Viable to sort IEEE754 floats by MSB?
I've been doing some low-level bit manipulation, and ended up creating an algorithm that sorts 64-bit floats by octets of descending significance (LE = 7 -> 0; BE = 0 -> 7) as a byproduct. When ...
0
votes
2
answers
170
views
Are bitwise operators slower than common loops like a for loop?
Do bitwise operators like & (AND) take more runtime than common for loops?
Today I was doing a power of 2 question in LeetCode. My code was this:
if (n > 0 && (n & (n - 1)) == 0) {
...
5
votes
1
answer
119
views
Logical shift right without dedicated shift instruction
I am working with an assembly language that does not contain either multiply, divide, or bit-shift instructions. I am aware that a left bit-shift can be achieved by just adding the same number to ...
0
votes
1
answer
43
views
Can't shift bits in a loop
I'm trying to make a program that shifts bits that go outside the range back to 0. I encountered this problem:
where it just prints out the same 3 sets of bits and ignores the changes that happen ...
1
vote
1
answer
94
views
How to turn a 16-bit number in one's complement into it's 7-segment display equivalence?
I have a 16-bit integer, in one's complement, and I'm trying to retrieve it's 7-seg display equivalence. Using only bitwise operations.
I'm doing it for a Minecraft project/hobby, just trying to build ...
2
votes
0
answers
74
views
Understanding Bitmask Immediates in ARM64 Assembly [duplicate]
I'm currently learning ARM64 assembly and I'm having trouble understanding how bitmask immediates are implemented and validated. I've read that ARM64 has specific rules for what constitutes a valid ...
0
votes
0
answers
34
views
Alternate approach to record bits of number in chunks
Problem link = https://www.naukri.com/code360/problems/convert-to-hexadecimal_1102544
My solution is following
string toHex(int num) {
string ans = "";
// Loop through the integer ...
2
votes
1
answer
213
views
Is bitwise shift more "efficient" than a for loop? [closed]
I just started going through "Data Structures and Algorithms in C++" and in one of the intro exercises they ask to write a function which returns powers of 2 and takes the exponent n
as an ...
0
votes
1
answer
108
views
Fastest way to sample weighted random index in C++?
I am implementing an algorithm which requires taking thousands of random samples from an array of 100 integers.
std::vector<int> weight_vector(100);
I would like to understand how I can improve ...
1
vote
0
answers
38
views
Parsing bitwise data big-endian way using Python struct module
I have a hex string (put together from a hex dump file) which needs to be parsed in big-endian order to read individual bits.
Here are some examples of message structure with the expected value for ...