# LaVOZs

The World’s Largest Online Community for Developers

'; Newest 'bit-manipulation' Questions - LavOzs.Com

# Questions tagged [bit-manipulation]

The manipulation of individual bits. Operators used may include bitwise AND, OR, XOR, NOT, left-shift, and right-shift.

• ### Binary Interleaving, Binary Swizzling, Alternating Bits

Problem: I have a sequence of bits of indices 7 6 5 4 3 2 1 0 and I want to swizzle them the following way : 7 6 5 4 3 2 1 0 = 7 6 5 4 3 2 1 0 _____| | | | | | | |_____ | ___| | | |...
• ### How do I use bitwise operators to accomplish this?

int a = 0b1011011011; int b = 0b1000110110; int c = 0b0101010101; int d = (a & b) ^ c; //Wrong Intended value ofd is 0b1010011110 I need to write d so that when the bit of c is 1, the ...
• ### Creating addresses from multiple values in C

I am trying to create 32 bit addresses in C by combining multiple unsigned int values I have. These multiple values are important and need to be all stored into 1 32 bit address. For example: Say I ...
• ### Operations on single bit and introduction of bit mask [closed]

How do we deal with single bits? We'll now show you what you can use bitwise operators for. Imagine that you're a developer obliged to write an important piece of an operating system. You've been told ...
• ### I’m having trouble to understand how to fill in the bits with the LBS to shrink numbers of bits

0C 1C 34 38 34 24 12 1C 24 10 1B 0E 0A 1D 25 C S 2 6 2 I S G R E A T ! Translating the above message into individal 8-bit bytes, we get: 00001100 00011100 ...
• ### Bitwise operation and stored function calls in Symfony and Doctrine respectively

I'm using Doctrine 2.7.1-DEV with Symfony CLI version v4.13.3. I have recently started working with Symfony. My task is reprogramming a web application. So there is an existing database that needs to ...
• ### find combinations of bytes [closed]

Here is my attempt at an immutable byte combiner, I got the idea from trying to copy BitArray with a Span<bool> and figure out it can almost be done inline. Is this a good method to achieve my ...
• ### I need a good integer hash function in C [closed]

I am looking for a good hash function with the following characteristics: The input is any unsigned int number and the output must be {0,...,N-1} where N must be an unsigned int parameter of the ...
• ### Is there a non-iterative method of toggling groups of bits on and off using a mask?

Suppose I've two bit strings: runs and toggler, where a run is a group of contiguous like bits. Both of these bit strings can have an arbitrary arrangement of 1s and 0s (on and off respectively). For ...
• ### Re-writing C++ bit manipulation function in Python

I'm attempting to convert some C++ code that I found online that is used for reordering a bitmap to be used with a specific OLED display for a MIDI controller - Akai Fire. The display uses a strange ...
• ### Encoding a decimal number within a range using bitmask

I have a usecase to encode number within range of -5.5 to +5.5 using a bitmask of 9 bits, and then apply predefined offset on it. After going through online tutorials, I come up with following ...
• ### Bit operation of cuda tensor in pytorch

I'd like to do some bit operation with torch.tensor in cuda, such as <<, >>, or extract each bit which represents a float number, such as 0 01101 01010101012 (3555H) for 0.333 in float16. what ...
• ### View a specific bit of a hex number?

I am working on a piece of logic that will teach me how bitops and bit manipulation works, and I am trying to view a specific bit of a given hex number. For example f0f0 has the following bit value ...
• ### bitwise operations with Z3 in python

once again I'm struggling with Z3. I'm trying to build a code from a binary I disassembled with IDA: This function is being called 0x80 times. ecx is initialized with 0x40. [rdi+8] initialized with ...
• ### javascript sharedArrayBuffer and bitwise operations returning a 32bit instead of 16bit number

javascript, 2020: I've never worked with the sharedArrayBuffer, (or bits) and I have some questions. Basically I want to store a set of bools and a small counter (4-bit), on a single Int16Array ...
• ### Check whether the input is the largest 2's complement integer

/* * isTmax - returns 1 if x is the maximum, two's complement number, * and 0 otherwise * Legal ops: ! ~ & ^ | + * Max ops: 10 * Rating: 1 */ int isTmax (int x) { int t = x + ...
• ### Divide 2 numbers without using / ,* or %

Let's say we assume a=16 b=3 First I am trying to find the x to which when I multiply 3 and then subtract is from 16 will get the min difference 16-3<<0 =>16-(3*1) -- 16-3<<1 =>16-(3*2) --...
• ### How 4<<1<<2 is 32?

when 1<<2 : 4 4<<1<<2 should be 4<<4 which is 64 but it is showing 32. I am new to bit manipulation, please let me know where I am doing wrong.
• ### Constant time n left bit-shift of n bits

For bitshifting large integers. I have seen this answer in StackOverflow: uint64_t i1, i2, i3, o1, o2, o3; // {o3, o2, o1} = {i3, i2, i1} << 3; o3 = i3 << 3 | i2 >> (32 - 3); o2 = ...
• ### How do I get all the bits that are on in one set of bits but not another

Say I have 2 sets of bits. Pretend that they are two sets of permissions Desired Permission Actual Permissions I want to know the desired permissions that are not set in actual permissions. Is ...
• ### bit wise counter on a single byte javascript

I want to use the bits valued at 2^4-2^8 in a Typed 16Uint array item to use as a binary counter to 16. 0000111100000000 ->15 0000111000000000 ->14 0000110100000000 ->13 ... 0000000000000000 ...
• ### & Bit operation in python [duplicate]

In doing a bitwise &, I thought by specifying the digit it would add that digit in the necessary spot, but in python I get the following: >>> 4&2 0 >>> 4&1<<1 0 &...
• ### How do we manipulate the first bit of a u8 array in Rust? [duplicate]

I'm trying to make an asynchronous WebSocket server (my own implementation). I have the following code: use tokio::net::TcpListener; #[tokio::main] async fn main() -> Result<(), Box<dyn std:...
• ### Using a long number to generate 2 unique long numbers

I have an application that uses a generated number, eventNumber, on an Event Message, as a message id to produce a Notification Message. eventNumber is guaranteed to be unique and increasing. So for ...
• ### Change ONLY the higher bit in MASM32 [duplicate]

How can I change only the higher bit of byte in masm32? Assume I have 00101111 byte and want to get 10101111.
• ### Understanding the use of bitwise operators in MySQL?

Can someone explain the purpose of using bitwise operators(like BIT_OR) in MySQL queries. For example, if have a table such as following: What is the purpose of aggregate operation like: SELECT name ...
• ### Python distribute 8 bits into beginnings of 4 x 8 bits, two by two

I have an integer that is 8 bits and I want to distribute these bits into beginnings of 4 integers (4x8 bit) two by two. For example: bit_8 = 0b_10_11_00_11 bit_32 = b"\x12\x32\x23\54" # --> [...
• ### How to set N amount of consecutive bits in a byte, starting from LSB? [duplicate]

I want to set an N amount of bits in a byte (byte always starts as 0) and store it using a pointer. Imagine: void SetBits(uint8_t bytesToSet, uint8_t* var) {} How would I go about implementing this (...
• ### Bitwise operations, comparing a u32 with a byte array

Lets say I have the value 1025 as a byte array and the value 1030 as usize. How would I go about comparing if the byte array is bigger, lesser or equal without deserializing it? I'm completely stuck, ...
• ### Can anyone simplify this bitwise expression

(a&b)^(a|b) how can I simply this, if you can add any reference for learning simplification it will be better
• ### How do you split a byte into sets of 2 and then rotate them using C? [closed]

I am currently learning bitwise operations in C. Given a byte(spaced for readability): 11 01 11 10 Must turn it into: 10 11 01 11 How do I go about doing this?
• ### How to check if two numbers on a 2D array are of different parity

In my real case I have an array of shape (8,2) and I need to check if two integers are of different parity. For example here I want that the line 0 return False (8 and 2 are even, so same parity) line ...
• ### Ambigious Behaviour of various c++ Compilers

When you do left shift of a -ve number on Leetcode it shows run time error as shown in the following screenshot leetcode compiler output Now For the Same code on GeeksForGeeks IDE, it works well as ...
• ### How can I swap 2 integers in C using bitwise operators and bit manipulation?

I am having some trouble with swapping 2 integers using bit manipulation. Below is my code and console input/output. #include <stdio.h> int main() { int num1 = 0; int num2 = 0; int ...
• ### How can I concatenate integer bit fields in c into a uint64_t integer?

I have a struct in c like this struct RegisterStruct { uint64_t b_0 : 64; uint64_t b_1 : 64; uint64_t c_0 : 64; uint64_t c_1 : 64; uint64_t c_2 : 64; uint64_t d_0 : 64; ...
• ### How to change the least significant bit in Python [duplicate]

I'm new in Python and I would like to know how to change the least significant bit in Python. I would like to do something like this: mybytes = bytes(16) ## Creating the byte object filled with ...
• ### Bits: Search&Replace bit sequences

Being a high-level programmer myself, I struggle a lot with bitwise operations. I hope what I'm trying to achieve is doable? Let's say I have one unsigned 8-bits integer - it can be any value. Let's ...
• ### Rewriting C-expression using << and +

How can I write following C expression using left-shift bit-wise operator (<<) and plus sign (+)? y = x * 6;
• ### C++ bitwise masking and shift

I have a legacy C++ binary operation code like this: constexpr static uint64_t val2bin(double val) { uint64_t bin = (val > 0.) ? uint64_t(val/0.01)+1 : 0; return (bin > 510) ? 511 : bin;...
• ### Packing bits in C using bitwise operators

Say I have an unsigned char array of ASCII characters with the binary values: 00001100 00011100 00110100 00111000 00110100 00100100 00010010 00011100 00100100 00010000 00011011 00001110 ...
• ### How to perform bitwise or operation between two binary numbers in Swift?

I need to do bitwise OR of two binary strings. For example, if the binary strings are "110001" and "101101", then I need the result as "111101". How can I do this in Swift ?
• ### Using bitmask+dynamic programming for solution of sum over subset problem [duplicate]

I found this question in geeksforgeeks related to dynamic programming with bitmasking I am trying for weeks but not able to understand how the answer is formulated. Please provide any links, that may ...
• ### How do you store and mutate a bitmap/bitset using MySQL?

I want to create a table column that stores a 500 byte bitmap (500 bytes * 8 bits per byte = 4000 bits) and do operations to mutate bits (set to 1 or 0) at certain indexes in the bitmap. However, ...
• ### How to specify 3GB using bit shift in python?

I am allocating memory in the Jetson TX2. It has 8GB of RAM. I need to specify the maximun GPU memory size available for TensorRT. max_workspace_size_bytes = (has to be an integer) I have seen ...
• ### 2-bit mapping using bitwise operations in C

This is my first question, so I hope to do this right. I have a problem where I have to map a key which can be in the range (0, 1, 2) to select a value from the same range (0, 1, 2). I have to repeat ...
• ### Java left shift clarification

I am unable to figure out the reason for this behavior. I want to left shift by 8 bits the byte value OxAB. Then I want to convert that to a long. byte oneByte = (byte) 0xAB; long converted = (...
• ### Verilog signal assignment conversion to VHDL

Currently I'm working on conversion Verilog module to VHDL and I've encountered a line which I am not sure how to convert to VHDL. I am talking about the last line of the code that is shown below. I ...
• ### Absolute value abs(x) using bitwise operators and Boolean logic [duplicate]

How does this work? The idea is to make abs(x) use bitwise operators for integers (assuming 32 bit words): y = x >> 31 (x + y) ^ y // This gives abs(x) (is ^ XOR)?
• ### OpenCV bitwise operation more than two image

In OpenCV, is there any way to bitwise operation more than two image in one line code? for example, result = cv.bitwise_and(source1, source2, source3, source4) something like that. not like mask1 ...
• ### How to think about Python's negative number bitwise operations?

I find it quite difficult to think about Python (and Python3)'s infinite precision negative numbers and bitwise operations. It is not 32-bit or 64-bit. The 1s at the left can be thought of as "...