bit-manipulation

Number of unset bit left of most significant set bit?

会有一股神秘感。 提交于 2019-12-04 05:10:39
Assuming the 64bit integer 0x000000000000FFFF which would be represented as 00000000 00000000 00000000 00000000 00000000 00000000 >11111111 11111111 How do I find the amount of unset bits to the left of the most significant set bit (the one marked with >) ? MSN // clear all bits except the lowest set bit x &= -x; // if x==0, add 0, otherwise add x - 1. // This sets all bits below the one set above to 1. x+= (-(x==0))&(x - 1); return 64 - count_bits_set(x); Where count_bits_set is the fastest version of counting bits you can find. See https://graphics.stanford.edu/~seander/bithacks.html

Generate matrix of bits

痞子三分冷 提交于 2019-12-04 04:33:51
问题 I would like to take an integer n defining the number of bits in my communication code and a vector defining the alphabet I am assigning to bits 0:n-1 , and output a matrix/cell array containing the alphabetic notation for each state, i.e.: function M = mycommarray(3,[-1,1]) produces M = [{-1,-1,-1}, {-1,-1,1}...] I tried doing this an easier way with dec2bin(0:7,3) , but there doesn't seem to be a quick way to make the zeros into -1 s. Is there anything close to prepackaged that does this?

Why does OR 0 round numbers in Javascript?

我的未来我决定 提交于 2019-12-04 04:21:14
问题 I'm under the impression that the Number type in Javascript stores any number, integer or float, according to the IEEE floating point standard. If so, then why does bitwise OR-ing a number with 0 round it down? Playing around with some other bit ops, it appears that when applying bit operations to floating point numbers, the number is first rounded towards 0 and then the bit operations are applied (with the numbers in Two's complement representation rather than IEEE). Is this correct? 回答1: In

regarding left shift and right shift operator

荒凉一梦 提交于 2019-12-04 04:09:10
问题 void times(unsigned short int time) { hours=time>>11; minutes=((time<<5)>>10); } Take the input time to be 24446 The output values are hours = 11 minutes = 763 The expected values are hours = 11 minutes = 59 What internal processing is going on in this code? Binary of 24446 is 0101111101111110 Time>>11 gives 01011 which means 11 . ((Time<<5)>>10) gives 111011 which means 59 . But what else is happening here? 回答1: What else is going on here? If time is unsigned short , there is an important

Bit masking in Python

帅比萌擦擦* 提交于 2019-12-04 03:55:16
I have a byte (from some other vendor) where the potential bit masks are as follows: value1 = 0x01 value2 = 0x02 value3 = 0x03 value4 = 0x04 value5 = 0x05 value6 = 0x06 value7 = 0x40 value8 = 0x80 I can count on ONE of value1 through value6 being present. And then value7 may or may not be set. value8 may or may not be set. So this is legal: value2 | value7 | value8 This is not legal: value1 | value3 | value7 I need to figure out whether value 7 is set, value8 is set, and what the remaining value is. I have the following python code. Is there a more elegant way to do this? value1 = 0x01 value2

k&r exercise confusion with bit-operations

一个人想着一个人 提交于 2019-12-04 03:31:25
问题 The exercise is: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. My attempt at a solution is: #include <stdio.h> unsigned setbits(unsigned, int, int, unsigned); int main(void) { printf("%u\n", setbits(256, 4, 2, 255)); return 0; } unsigned setbits(unsigned x, int p, int n, unsigned y) { return (x >> (p + 1 - n)) | (1 << (n & y)); } It's probably incorrect, but am I on the right path

How to get lg2 of a number that is 2^k

╄→尐↘猪︶ㄣ 提交于 2019-12-04 03:31:10
What is the best solution for getting the base 2 logarithm of a number that I know is a power of two ( 2^k ). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) = bitcount( n - 1 ) = k, iff k is an integer 0b10000 - 1 = 0b01111, bitcount(0b01111) = 4 But is there a faster way of doing it (without caching)? Also something that doesn't involve bitcount about as fast would be nice to know? One of the applications this is: suppose you have bitmask 0b0110111000 and value 0b0101010101 and you are interested of (value &

How does this bit manipulation work in Java?

寵の児 提交于 2019-12-04 03:28:02
I was looking into how Java counts bits set of an int. I had in my mind something simple like this (which I think is correct): public static int bitCount(int number){ final int MASK = 0x1; int count = 0; for(int i = 0; i < 32; i++){ if(((number >>> i) & MASK) == MASK){ count++; } } return count; } Instead I found a method that I absolutely have no idea what is doing (seems like magic to me): i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; Could someone help understand

Bitwise flags and Switch statement?

怎甘沉沦 提交于 2019-12-04 03:25:13
I have the following code (example), and I'm really not comfortable with so many 'if' checks: public enum Flags { p1 = 0x01, // 0001 p2 = 0x02, // 0010 p3 = 0x04, // 0100 p4 = 0x08 // 1000 }; public static void MyMethod (Flags flag) { if ((flag & Flags.p1) == Flags.p1) DoSomething(); if ((flag & Flags.p2) == Flags.p2) DosomethingElse(); if ((flag & Flags.p3) == Flags.p3) DosomethingElseAgain(); if ((flag & Flags.p4) == Flags.p4) DosomethingElseAgainAndAgain(); } MyMethod(Flags.p1 | Flags.p3); Is there some way, that i could use an 'switch' statement. Maybe if I convert them to strings, or use

How does this color blending trick work?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 03:22:35
I saw this Java code that does a perfect 50% blend between two RGB888 colors extremely efficiently: public static int blendRGB(int a, int b) { return (a + b - ((a ^ b) & 0x00010101)) >> 1; } That's apparently equivalent to extracting and averaging the channels individually. Something like this: public static int blendRGB_(int a, int b) { int aR = a >> 16; int bR = b >> 16; int aG = (a >> 8) & 0xFF; int bG = (b >> 8) & 0xFF; int aB = a & 0xFF; int bB = b & 0xFF; int cR = (aR + bR) >> 1; int cG = (aG + bG) >> 1; int cB = (aB + bB) >> 1; return (cR << 16) | (cG << 8) | cB; } But the first way is