bit-manipulation Questions

2

Solved

We are using bit fields 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 dev...
Waken asked 11/7 at 13:25

1

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 i...

15

Solved

Given a decimal integer (eg. 65), how does one reverse the underlying bits in Python? i.e.. the following operation: 65 → 01000001 → 10000010 → 130 It seems that this task can be broken down into ...
Finisterre asked 1/10, 2012 at 22:20

3

Solved

A handy method to verify if a positive integer n is a power of two (like 1, 2, 4, 8, etc.) is to use the following test for having no more than 1 bit set: bool test = n & (n - 1) == 0; This op...

6

Solved

I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them...

6

Solved

We all know that the logical && operator short circuits if the left operand is false, because we know that if one operand is false, then the result is also false. Why doesn't the bitwise &...
Mortification asked 13/3, 2012 at 16:46

3

Solved

The context of this question is the creation of a side-channel resistant implementation of a IEEE-754 compliant single-precision square root for a 32-bit RISC-V platform without hardware support fo...

4

Solved

I have a Javascript integer (whose precision can go up to 2^53 - 1), and I am trying to send it over the wire using an ArrayBuffer. I suppose I could use BigInt64Array, but the browser support stil...
Incautious asked 2/6, 2022 at 12:6

19

Solved

I was thinking how to get the absolute value of an integer without using if statement nor abs(). At first I was using shift bits left (<<), trying to get negative sign out of the range, then ...
Subtlety asked 19/3, 2012 at 14:52

16

Solved

There is a lot of information on how to find the next power of 2 of a given value (see refs) but I cannot find any to get the previous power of two. The only way I find so far is to keep a table w...
Speech asked 21/4, 2010 at 1:50

11

Solved

I'm looking at some code which should be trivial -- but my math is failing me miserably here. Here's a condition that checks if a number if a power of 2 using the following: if((num != 1) &&a...
Lyons asked 27/6, 2009 at 20:44

1

The task is like How to set bits of a bit vector efficiently in parallel?, but for CUDA. Consider a bit vector of N bits in it (N is large, e.g. 4G) and an array of M numbers (M is also large, e.g....
Picofarad asked 27/7, 2022 at 7:12

3

Solved

I was working on an algorithm of sub sequences. What is the meaning of the statement: if (counter & (1<<j)) within the context of the program below: void printSubsequences(int arr[], int...

9

How do I change the most significant bit in an int from 1 to 0? For instance, I want to change 01101 to 0101.
Gainsay asked 17/10, 2011 at 6:46

1

Solved

This question was inspired by a question I recently came across on Stackoverflow. It reminded me that early in the development of the x86-64 ISA I had written 32-bit x86 code for BCD addition witho...

34

If I have some integer n, and I want to know the position of the most significant bit (that is, if the least significant bit is on the right, I want to know the position of the farthest left bit th...
Oleg asked 22/3, 2009 at 23:37

9

Solved

I want to extract bits of a decimal number. For example, 7 is binary 0111, and I want to get 0 1 1 1 all bits stored in bool. How can I do so? OK, a loop is not a good option, can I do something ...
Inky asked 12/2, 2010 at 4:51

4

Solved

I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt ...
Portia asked 22/6, 2017 at 9:6

12

Solved

Write a branchless function that returns 0, 1, or 2 if the difference between two signed integers is zero, negative, or positive. Here's a version with branching: int Compare(int x, int y) { int...
Sanatory asked 23/10, 2009 at 0:38

4

Solved

I have a of list of bitwise elements, e.g. [1,1,1], and I want to do a bitwise OR operation between every element in the list. So, e.g. for [1,1,1] do 1 | 1 | 1 = 1 or for [1,17,1] do 1 | 17 ...
Cumulonimbus asked 11/9, 2014 at 20:15

4

I have a square boolean matrix M of size N, stored by rows and I want to count the number of bits set to 1 for each column. For instance for n=4: 1101 0101 0001 1001 M stored as { { 1,1,0,1}, {0...
Laaspere asked 23/7, 2018 at 9:37

9

I need to extract specific part (no of bits) of a short data type in C. For Example I have a binary of 52504 as 11001101000 11000 and I want First 6 ( FROM LSB --> MSB i.e 011000 decimal 24) bits...
Conqueror asked 10/4, 2012 at 14:5

16

I'm converting an unsigned integer to binary using bitwise operators, and currently do integer & 1 to check if bit is 1 or 0 and output, then right shift by 1 to divide by 2. However the bits a...
Calore asked 4/2, 2012 at 21:42

7

I want to know which value the first bit of a byte has. For example: I have byte m = (byte) 0x8C; How could I know if the first bit is an 1 or a 0 ? Can anyone help me out ?
Oestrone asked 28/9, 2011 at 17:16

31

Solved

I want to write a function that returns the nearest next power of 2 number. For example if my input is 789, the output should be 1024. Is there any way of achieving this without using any loops but...
Eastwood asked 21/1, 2009 at 17:26

© 2022 - 2024 — McMap. All rights reserved.