bit-manipulation

Changing specific set of bits in a byte

自古美人都是妖i 提交于 2019-12-11 15:03:21
问题 I am working on a function that receives a byte and needs to change some of the bits in that byte. For example, the function receives: 11001011 Then I need to set the MSB to 0, its easy enough: buffer[0] &= ~(1 << 7); But then I need to set bits 6 through 3 (I refer LSB as bit 0 here) to an argument that gets supplied to the function. This argument can be an integer from 0 to 6 . The important thing is I should not change any other bits. I tried with masking and stuff but I failed miserably.

Binary trees: Getting the path of an element from its signature

半腔热情 提交于 2019-12-11 14:19:39
问题 Assume you have a binary tree which classifies images. Each node is a different binary test. When an image is fed to the tree, a unique path through the tree is generated. A path is described as a binary word which is as long as the depth of the tree. For instance, for a 2-stage binary tree, an example of path would be (0,1) ((left,right) thus ending in the second leaf of the tree from the left). We also assume that for any image being fed to the tree, all node tests are executable. Thus, we

Bitwise enum cast return value not expected

Deadly 提交于 2019-12-11 11:45:42
问题 I have the following enum: [Flags] public enum PermissionLevel { User = 1, Administrator = 2, ITStaff = 3, Manager = 4, SuperAdministrator = 6, } When I do: PermissionLevel permission = (PermissionLevel) dr.GetInt32(i); I get random permission values assigned to the permission object. For instance, if i is 6, my permission object returns "Administrator | Manager" and I'm supposed to get "SuperAdministrator". When I cast the instance back to integer it returns 6. Am I missing something? 回答1:

Shifting big numbers

こ雲淡風輕ζ 提交于 2019-12-11 11:29:10
问题 X = 712360810625491574981234007851998 is represented using a linked list and each node is an unsigned int Is there a fast way to do X << 8 X << 591 other than X * 2^8 X * 2^591 ? 回答1: Bit shifting is very easy in any arbitrary number of bits. Just remember to shift the overflowed bits to the next element. That's all Below is a left shift by 3 example uint64_t i1, i2, i3, o1, o2, o3; // {o3, o2, o1} = {i3, i2, i1} << 3; o3 = i3 << 3 | i2 >> (32 - 3); o2 = i2 << 3 | i1 >> (32 - 3); o1 = i1 << 3

Left shift operator

五迷三道 提交于 2019-12-11 10:47:31
问题 If I have the following: char v = 32; // 0010 0000 then I do: v << 2 the number becames negative. // 1000 0000 -128 I read the standard but it is only written: If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. so I don't understand if is a rule that if a bit goes on most left bit the number must begin negative. I'm using GCC. 回答1: Left shifting it twice would give 1000 0000)

How to randomly pick a value based on the position of the bit

旧街凉风 提交于 2019-12-11 10:21:56
问题 Is there a way to pick a value based on the bit position. The problem statement is:- for a 16 bits position, I can set any bits, say I set 1,4,6,7,11,13 bit so the mask would be:- Bit Positons 0 0 1 0 1 0 0 0 1 1 0 1 0 0 1 0 Now I need to randomly pick a value based on this bit mask, where only 1 bit is set, so my possible values could be:- For selecting 4 :0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 For Selecting 7: 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 But I need to select this value randomly, so I though of

What's the fastest method to return the position of the least significant bit set in an integer in Python 3?

断了今生、忘了曾经 提交于 2019-12-11 10:10:16
问题 I'm curious to find what the fastest algorithm is for returning the position of the least significant bit set in an integer in Python 3. Are there algorithms faster than this one in Python 3? Any enhancements one could use to speed things up? def lsb(n): temp = n & -n pos = -1 while temp: temp >>= 1 pos += 1 return(pos) 回答1: summarizing, since this is for python3 and so not an exact duplicate of return index of least significant bit in Python (although there are other applicable answers there

Find missing number from 4 billion (again)

无人久伴 提交于 2019-12-11 09:53:33
问题 It seems that a question that has been asked a lot of times is about to detect a missing number among 4 billion numbers. The recomended approach appears to be to use a bitset (when memory constraints are part of the problem). An example post is this:find-an-integer-not-among-four-billion-given-ones and I could also link to more here in SO. My problem is the following: The bitset approach seems to implicitely asume that the numbers are non -negative. As an example in the post I linked to,

4 float color to one float in java, and back again in openGL ES 2.0 shader

倾然丶 夕夏残阳落幕 提交于 2019-12-11 08:49:29
问题 I am trying to send one color with every vertex that goes in to the shader, but in only one float value. I think is weird that you cannot send 4 bytes as attributes with every vertex, but sense it's not possible I am going to try to pack RGBA in a single float variable, so this is my code: Jave code (that packs the values in one float): private float fourfColor2One(float r, float g, float b, float a) { long temp = (byte) (r * 255); float res = temp << 24; temp = (byte) (g * 255); res += temp

function to convert float to int (huge integers)

这一生的挚爱 提交于 2019-12-11 08:16:52
问题 This is a university question. Just to make sure :-) We need to implement (float)x I have the following code which must convert integer x to its floating point binary representation stored in an unsigned integer. unsigned float_i2f(int x) { if (!x) return x; /* get sign of x */ int sign = (x>>31) & 0x1; /* absolute value of x */ int a = sign ? ~x + 1 : x; /* calculate exponent */ int e = 0; int t = a; while(t != 1) { /* divide by two until t is 0*/ t >>= 1; e++; }; /* calculate mantissa */