bit-manipulation

How to read off 1 flag bit and get integer at same time

筅森魡賤 提交于 2019-12-25 02:29:14
问题 Say I have an 8-bit number with a flag at either side: <flag>0101011 (decimal 43) 0101011<flag> (decimal 43) Not sure which is more optimal for what's next. Then say I want to check the flag, and then use the number. Wondering if that can be done in one operation. Or if not, the fewest operations it can be done in. 回答1: You could do something like this using the flag at the end: var num = 0b01010110; var flag = num & 1; var int = num >> 1; console.log(flag); console.log(int); But binary

How to determine the number of right bit-shifts needed for a power of two value?

岁酱吖の 提交于 2019-12-25 02:26:19
问题 I have a function that receives a power of two value. I need to convert it to an enum range (0, 1, 2, 3, and so on), and then shift it back to the power of two range. 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024 ... and so on. If my function receives a value of 1024, I need to convert it to 10. What is the best way to do this in C#? Should I just keep dividing by 2 in a loop and count the iterations? I know I can put it back with (1 << 10). 回答1: Just use the logarithm of base 2:

Implementing a logical shift right

喜欢而已 提交于 2019-12-25 01:37:14
问题 So I'm working on the nand2tetris project, and I want to implement shift right logical on a software level since the hardware doesn't support it. I know shift right logical is a division by two. So my first shot at implementing it would count the number of times I was able to subtract 2 from the initial value before the value became 0 or negative. Similar for if the number was negative. But, I've found a scenario where it's not working. I want to shift right -27139. Well the binary value

How to use bitwise operators to return bits that have 0

淺唱寂寞╮ 提交于 2019-12-24 22:45:59
问题 I've been struggling for quite a long time on this problem. This is a follow-up question on this answer (How to divide x number of players into 2 teams randomly multiple times, differently every time?). So, I have x number of players, and for each I give 1 << n mask value. By using those masks I can easily form a match with 2 players in each team. Now, if total number of players is 5, one possible match could be like this: 01100 team a 00011 team b ------------ 10000 player resting or with 6

Need help writing a binary reader extension method for a specific value format: 6 bit then 7 bits structure

北慕城南 提交于 2019-12-24 21:38:18
问题 Alright so here goes. I currently need to write an extension method for the System.IO.BinaryReader class that is capable of reading a specific format. I have no idea what this format is called but I do know exactly how it works so i will describe it below. Each byte that makes up the value is flagged to indicate how the reader will need to behave next. The first byte has 2 flags, and any subsequent bytes for the value have only 1 flag. First byte: 01000111 ^^^^^^^^ |||____|_ 6 bit value ||___

How to compute the height profile of a Tetris stack most efficiently?

两盒软妹~` 提交于 2019-12-24 17:18:22
问题 Problem Statement We're given an array of Integers stack of length height . The width tells us that at most the width -lowest bits in each entry of xs are set. Compute an array profile of length width such that profile[i] == max_i with: max_i is maximal with stack[max_i] having the i -th bit set. How can I achieve this in a more efficient way than below? Current solution Currently I go over the columns and check each bit separately. The following shows my current implementation in Scala. But

Creating a mask around a subsection [i,j] for a number [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-24 14:22:07
问题 This question already has answers here : Fastest way to produce a mask with n ones starting at position i (5 answers) Closed 7 months ago . I'm learning bit manipulation and bitwise operators currently and was working on a practice problem where you have to merge a subsection[i,j] of an int M into N at [i,j]. I created the mask in a linear fashion but after googling i found that ~0 << j | ((1 << i) - 1) creates the mask I wanted. However, I am not sure why. If anyone could provide

Setting the nibble for an int - java [duplicate]

好久不见. 提交于 2019-12-24 12:52:54
问题 This question already has answers here : set 4-bit nibble in an int type (3 answers) Closed 3 years ago . I have to set the nibble for an integer using java. Honestly I am confused on how to shift/set/change a nibble to an nibble that I want. My TA told me it is supposed to be about 5 lines of code but I don't know how to even start it. Any help is greatly appreciated. /* Examples: * setNibble(0xAAA5, 0x1, 0); // => 0xAAA1 * setNibble(0x56B2, 0xF, 3); // => 0xF6B2 * * @param num The int that

How to load 12 bit image into System.Drawing.Bitmap?

∥☆過路亽.° 提交于 2019-12-24 12:26:25
问题 I have a server serving 12bit image and I can access the image through http, but since it is 12 bit image I am not able to load into System.Drawing.Bitmap(Stream stream). Image format PNG bit depth 12 Image is here original 12 bit one This is how the image should look once you are able to Load. 回答1: You may be able to read the image when using OpenCV library, since it also supports 12bit. You may also prefer to look for an adapter/wrapper, in order to use it in c#, since it is originally a C+

Aggregate bitfield values with binary OR

扶醉桌前 提交于 2019-12-24 11:28:40
问题 I have a table with int values being used as bitfields (where each bit is a flag). Now I would like to aggregate them with a binary operation (in my case OR) so that: SELECT 1 AS bitfield INTO #TABLE UNION ALL SELECT 1 + 2 + 8 + 32 UNION ALL SELECT 2 + 128 UNION ALL SELECT 2 + 32 SELECT AND_AGGR(bitfield) -- Invalid because AND_AGGR doesn't exist FROM #TABLE DROP #TABLE would result in the value 171 What would be a good way to do this that hopefully doesn't require a lot of | and MAX (but if