bit-manipulation

Conditional Using Bitwise Operators

最后都变了- 提交于 2019-12-09 06:38:12
问题 How is the conditional operator represented using bitwise operators? It is a homework question where I have to implement the conditional operator using only bitwise operations. It would be simple if if statements were allowed, however it has to be strictly bitwise operators. Only the operators ! , ~ , & , ^ , | , + , >> , and << can be used. No if statements or loops can be used. The function takes three ints and works just like the normal conditional operator. The first argument is evaluated

Is a bit field any more efficient (computationally) than masking bits and extracting the data by hand?

孤街醉人 提交于 2019-12-09 06:07:57
问题 I have a numerous small pieces of data that I want to be able to shove into one larger data type. Let's say that, hypothetically, this is a date and time. The obvious method is via a bit field like this. struct dt { unsigned long minute :6; unsigned long hour :5; unsigned long day :5; unsigned long month :4; unsigned long year :12; }stamp; Now let's pretend that this thing is ordered so that things declared first are at bits of higher significance than things declared later so if I represent

Explanation of the safe average of two numbers

大城市里の小女人 提交于 2019-12-09 05:30:03
问题 Whenever I need to average two numbers for an algorithm like binary search, I always do something like this: int mid = low + ((high - low) / 2); I recently saw another way to do it in this post, but I don't understand it. It says you can do this in Java: int mid = (low + high) >>> 1; or this in C++: int mid = ((unsigned int)low + (unsigned int)high)) >> 1; The C++ version essentially makes both operands unsigned, so doing a shift results in an arithmetic shift instead of a signed shift. I

algorithm behind the generation of the reverse bits lookup table(8 bit)

牧云@^-^@ 提交于 2019-12-09 04:28:00
问题 I found the lookup table here. The table is generated as a reverse bits table of 8 bits. I can not figure out why it works. Please explain the theory behind it. Thanks static const unsigned char BitReverseTable256[256] = { # define R2(n) n, n + 2*64, n + 1*64, n + 3*64 # define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16) # define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 ) R6(0), R6(2), R6(1), R6(3) }; 回答1: First off a comment: This kind of thing is normally only done in

Efficient way to set first N or last N bits of __m256i to 1, the rest to 0

两盒软妹~` 提交于 2019-12-09 03:09:38
问题 How to set to 1 efficiently with AVX2 first N bits last N bits of __m256i , setting the rest to 0 ? These are 2 separate operations for tail and head of a bit range, when the range may start and end in the middle of __m256i value. The part of the range occupying full __m256i values is processed with all- 0 or all- 1 masks. 回答1: The AVX2 shift instructions vpsllvd and vpsrlvd have the nice property that shift counts greater than or equal to 32 lead to zero integers within the ymm register. In

What is the fastest way to calculate the number of bits needed to store a number

谁说我不能喝 提交于 2019-12-09 02:59:30
问题 I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code. if (n == -1) return 32; if (n == 0) return 1; int r = 0; while (n) { ++r; n >>= 1; } return r; 回答1: You're looking to determine the integer log base 2 of a number (the l=highest bit set). Sean Anderson's "Bit Twiddling Hacks" page has several methods ranging from the obvious counting bits in a loop to

Precise control over texture bits in GLSL

自古美人都是妖i 提交于 2019-12-09 02:51:43
问题 I am trying to implement an octree traversal scheme using OpenGL and GLSL, and would like to keep the data in textures. While there is a big selection of formats to use for the texture data (floats and integers of different sizes) I have some trouble figuring out if there is a way to have more precise control over the bits and thus achieving greater efficiency and compact storage. This might be a general problem, not only applying to OpenGL and GLSL. As a simple toy example, let's say that I

How to insert zeros between bits in a bitmap?

北城余情 提交于 2019-12-09 02:46:42
问题 I have some performance-heavy code that performs bit manipulations. It can be reduced to the following well-defined problem: Given a 13-bit bitmap, construct a 26-bit bitmap that contains the original bits spaced at even positions . To illustrate: 0000000000000000000abcdefghijklm (input, 32 bits) 0000000a0b0c0d0e0f0g0h0i0j0k0l0m (output, 32 bits) I currently have it implemented in the following way in C: if (input & (1 << 12)) output |= 1 << 24; if (input & (1 << 11)) output |= 1 << 22; if

Unsigned / Signed Arithmetic Problems from A Programmer's Perspective Textbook

随声附和 提交于 2019-12-09 01:57:37
问题 int x = random(); int y = random(); unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it always yields 1, describe the underlying mathematical principles. Otherwise, give an example of arguments that make it yield 0. A. (x<y) == (-x>-y) B. ((x+y)<<4) + y-x == 17*y+15*x C. ~x+~y+1 == ~(x+y) D. (ux-uy) == -(unsigned)(y-x) E. ((x >> 2) << 2) <= x For these questions, I got that

find the index of the highest bit set of a 32-bit number without loops obviously

不羁的心 提交于 2019-12-09 01:42:49
问题 Here's a tough one(atleast i had a hard time :P): find the index of the highest bit set of a 32-bit number without using any loops. 回答1: With recursion: int firstset(int bits) { return (bits & 0x80000000) ? 31 : firstset((bits << 1) | 1) - 1; } Assumes [31,..,0] indexing Returns -1 if no bits set | 1 prevents stack overflow by capping the number of shifts until a 1 is reached (32) Not tail recursive :) 回答2: Floor of logarithm-base-two should do the trick (though you have to special-case 0).