bit-manipulation

Equivalent of De Bruijn LSB, but for MSB

ε祈祈猫儿з 提交于 2019-12-11 05:23:37
问题 Does anyone know of an algorithm similar to De Bruijn's LSB, but for MSB? Or alternately the most efficient way of determining the MSB? I know Log_2(Val) will do this, but I don't know if it's the most efficient method. The reason I need it is I need to convert little-endian to big-endian. I know the standard algorithm for this. However, the input is 64 bit, but typically the numbers will be 16 or 24 bit, so swapping the whole 8 bytes around is unneeded 99.9% of the time. 回答1: Isn't this

How to store a 64 bit integer in two 32 bit integers in Ruby

ⅰ亾dé卋堺 提交于 2019-12-11 05:07:29
问题 As the title says, I'm a little lost on how to accomplish this in Ruby...there are number of topics on how to do this in C or C++. Any ruby experts out there that can chime in on this? 回答1: The same syntax you'd use in C works in ruby, just drop the typecasts: n = 0xFFFFFFFFEEEEEEEE x = (n & 0xFFFFFFFF00000000) >> 32 y = n & 0xFFFFFFFF puts x.to_s(16) # => "ffffffff" puts y.to_s(16) # => "eeeeeeee" v = x << 32 | y puts v.to_s(16) # => "ffffffffeeeeeeee" If you need the values to be in chunks

Using bitwise & instead of modulus operator to randomly sample integers from a range

好久不见. 提交于 2019-12-11 04:39:58
问题 I need to randomly sample from a uniform distribution of integers over the interval [LB,UB] in C++. To do so, I start with a "good" RN generator (from Numerical Recipes 3rd ed.) that uniformly randomly samples 64-bit integers; let's call it int64() . Using the mod operator, I can sample from the integers in [LB,UB] by: LB+int64()%(UB-LB+1); The only issue with using the mod operator is the slowness of the integer division. So, I then tried the method suggested here, which is: LB + (int64()&

Bit Counting in C similar to bit twiddling hack

佐手、 提交于 2019-12-11 04:26:52
问题 I need to make a routine that counts bits in a word that does not involve loops (only bit operations), and does not use large constants. int x = 0xFFFFFFFF; x += (~((x >> 1) & 0x55555555)+1); x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); x = (((x >> 4) + x) & 0x0F0F0F0F); x += (x >> 8); x += (x >> 16); return(x & 0x0000003F); This I found on bit twiddling hacks, but the largest constant I can use is 0xFF... Not sure how to do this otherwise. Thanks folks. 回答1: You can for example use a

Integer limit crossed while performing bitwise left shift operation in array

半腔热情 提交于 2019-12-11 04:26:16
问题 I am creating a numpy array using the bitwise left shift operator. For example, I create array p, where the shape of array is same as that of matrix a i.e. (23,): >>> import numpy >>> a = numpy.array([0,0,0,0,0,0,1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,1,1]) >>> p = 1 << arange(a.shape[-1] - 1) #left shift And the result is as expected: >>> p array([ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152]) But, if we increase the

Unwanted sign extension in Arduino

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:24:39
问题 I am trying to achieve logical right shift in Arduino(i.e. avoid sign extension), and after reading the Arduino BitShift guide (https://www.arduino.cc/en/Reference/Bitshift), it suggests that shifting unsigned variables to the right, wont cause sign extension: When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have

Hex to ASCII in c

和自甴很熟 提交于 2019-12-11 04:00:51
问题 Here is my logic, to convert HEX to ASCII conversion in C: for (i=0;i<ArraySize;i++) { /*uses a bitwise AND to take the top 4 bits from the byte, 0xF0 is 11110000 in binary*/ char1 = Tmp[i] & 0xf0; char1 = char1 >> 4; /*bit-shift the result to the right by four bits (i.e. quickly divides by 16)*/ if (char1 >9) { char1 = char1 - 0xa; char1 = char1 + 'A'; } else char1 = char1 + '0'; Loc[j]=char1; j++; /*means use a bitwise AND to take the bottom four bits from the byte, 0x0F is 00001111 in

Sign extending from a constant bit width in C#

你离开我真会死。 提交于 2019-12-11 03:52:33
问题 I have a value thats 5 bits in length. 4 bits determine the number and the 5th bit determines the sign, there by holding any value between -16 and +15. How can I accomplish sign extending from a constant bit width in C#? I know in C, I can use something like the follow to accomplish this: int x; // convert this from using 5 bits to a full int int r; // resulting sign extended number goes here struct {signed int x:5;} s; r = s.x = x; How can I do something similar to this in C#? 回答1: It's not

How to multiply an int with a fraction

青春壹個敷衍的年華 提交于 2019-12-11 03:39:31
问题 I need to multiply an int by a fraction using bitwise operators without loops and such. For example, I need to multiply by x by 3/8. I thought you would: int value = (x << 1) + x; // Multiply by 3 value = (value >> 3); // Divide by 8 But that doesnt work. I tried googling binary times fraction but that give floating point examples. I dont know exactly if this homework is for floating point but my hunch is no but getting me prepared for it. So any suggestions? I need to round toward zero so

Time complexity of recursive algorithm with two recursive calls

余生长醉 提交于 2019-12-11 03:28:55
问题 I am trying to analyze the Time Complexity of a recursive algorithm that solves the Generate all sequences of bits within Hamming distance t problem. The algorithm is this: // str is the bitstring, i the current length, and changesLeft the // desired Hamming distance (see linked question for more) void magic(char* str, int i, int changesLeft) { if (changesLeft == 0) { // assume that this is constant printf("%s\n", str); return; } if (i < 0) return; // flip current bit str[i] = str[i] == '0' ?