bit-manipulation Questions

8

Solved

I want to create a wrapper around the x86 instructions PDEP (Parallel Bit Deposit) and PEXT (Parallel Bit Extract). On architectures where these aren't available (and the corresponding intrinsics a...
Boxer asked 17/1 at 16:54

8

Solved

I need to generate a random number, but it needs to be selected from the set of binary numbers with equal numbers of set bits. E.g. choose a random byte value with exactly 2 bits set... 00000000 -...

11

Solved

I am working through a problem which I was able to solve, all but for the last piece—I am not sure how one can do multiplication using bitwise operators: 0*8 = 0 1*8 = 8 2*8 = 16 3*8 = 24 4*8 =...
Indicative asked 15/9, 2010 at 21:33

3

Solved

In C, many operations employ bit shifting, in which an integer literal is often used. For example, consider the following code snippet: #define test_bit(n, flag) (1UL << (n) & (flag)) As...
Eccentricity asked 15/1 at 16:46

6

I need to write some logic to determine, given an even number. The highest power of two that evenly divides it. What is the maximum value of 2^n where Input % 2^n == 0? IE: Input -> Output 4 (010...
Cannonade asked 11/10, 2009 at 21:7

1

We consider a bit matrix (n x m) to be a regular array containing n lines of integers of size m. I have looked in Hacker's Delight and in other sources and the algorithms I found for this were rath...

16

Solved

I am looking for an efficient (optionally standard, elegant and easy to implement) solution to multiply relatively large numbers, and store the result into one or several integers : Let say I have...
Bautista asked 29/11, 2009 at 12:14

4

Solved

On the Bit Twiddling Hacks website the following algorithm is provided to round up an integer to the next power of two: unsigned int v; // compute the next highest power of 2 of 32-bit v v--; v |=...

6

Solved

I'm a little confused by the ~ operator. Code goes below: a = 1 ~a #-2 b = 15 ~b #-16 How does ~ do work? I thought, ~a would be something like: 0001 = a 1110 = ~a why not?
Fiume asked 2/9, 2011 at 2:44

11

Solved

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators)... At a core level, what does bit-shifting (<<, &gt...

10

Solved

I need to convert an int to a byte[] one way of doing it is to use BitConverter.GetBytes(). But im unsure if that matches the following specification: An XDR signed integer is a 32-bit datum th...
Kandi asked 23/8, 2009 at 16:22

4

Solved

Before flagging this as a duplicate, please read below, and check my code * my updated code! So my problem is that, I have to implement Java/JavaScript '>>>' (Unsigned Right Shift / Zero-fill Righ...
Enfield asked 14/12, 2016 at 3:23

4

Solved

Hi all I'm trying to divide by an unsigned constant using only shifts and adds/subtracts - I have no problem with this if it were multiplication, but I'm a bit stumped by the division. For exampl...
Petulancy asked 3/2, 2011 at 12:42

16

Solved

How can I multiply and divide using only bit shifting and adding?
Fabre asked 5/5, 2010 at 19:35

9

Solved

I have 8 bool variables, and I want to "merge" them into a byte. Is there an easy/preferred method to do this? How about the other way around, decoding a byte into 8 separate boolean values? I c...
Rotl asked 11/12, 2011 at 0:53

4

Solved

This is more of a just for fun question. I’m working on a SC61860 CPU, which is an 8-bit CPU for a Sharp PC-1360 Pocket Computer from 1987 (also used in PC-1401 & 1403’s). Its instruction set d...

6

Solved

My solution (for every bit of the input block, there is such a line): *parity ^= (((x[0] >> 30) & 0x00000001) * 0xc3e0d69f); All types are uint32. This line takes the second bit of the i...
Nedanedda asked 17/11, 2010 at 20:4

9

Solved

I am trying to convert a uint16_t input to a uint32_t bit mask. One bit in the input toggles two bits in the output bit mask. Here is an example converting a 4-bit input to an 8-bit bit mask: Inpu...
Roentgenotherapy asked 10/8, 2016 at 19:22

6

Solved

If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this: ...
Rubierubiginous asked 10/3, 2013 at 6:37

1

#include <cassert> #include <cstdint> uint64_t pos_of_nth_bit(uint64_t X, uint64_t bit) { while (X) { if (!bit--) return __builtin_ctzll(X); X = X & (X - 1); } assert(f...
Cristinacristine asked 13/11, 2023 at 18:11

4

I watched a youtube video on the Top 10 Craziest Assembly Language Instructions and some of these instructions have no obvious application to me. What's the point of something like PEXT, which take...
Indophenol asked 14/11, 2021 at 19:20

10

Solved

I'm looking for a fast way to compute a 3D Morton number. This site has a magic-number based trick for doing it for 2D Morton numbers, but it doesn't seem obvious how to extend it to 3D. So basica...
Hilel asked 21/6, 2009 at 21:8

3

I need a C macro to get the smallest of power two greater than a given number. For example, FIRSTFREEBIT(0x16) (binary 1_0110) must be equal to 0x20. I am going to use it as: #include <somehe...
Smail asked 30/5, 2014 at 12:28

5

Solved

Given an integer , print the next smallest and next largest number that have the same number of 1 bits in their binary representation After Counting the number of 1's in the number.How to determin...
Doorplate asked 31/3, 2011 at 10:2

3

Solved

From Hacker's Delight: 2nd Edition: The formula here seems a little bit awkward here. How is some x vector is subtracted from 1 vector (presumbly 0x1111 1111) when x is smaller than 1? (Like: (as...
Backpedal asked 30/10, 2020 at 8:38

© 2022 - 2024 — McMap. All rights reserved.