bit-manipulation

Finding the next in round-robin scheduling by bit twiddling

烂漫一生 提交于 2019-12-02 19:40:38
Consider the following problem. You have a bit-string that represents the current scheduled slave in one-hot encoding. For example, "00000100" (with the leftmost bit being #7 and rightmost #0) means that slave #2 is scheduled. Now, I want to pick the next scheduled slave in a round-robin scheduling scheme, with a twist. I have a "request mask" which says which slaves actually want to be scheduled. The next slave will be picked only from those that want to. Some examples (assume round-robin scheduling is done by rotating left). Example1: Current: "00000100" Mask: "01100000" Next schedule:

How to use bitshifting in Java

坚强是说给别人听的谎言 提交于 2019-12-02 19:08:39
问题 I am trying to construct an IP header. An IP header has the following fields: Version, IHL, DSCP etc. I would like to populate a Byte Array such that I can store the information in bytes. Where I get confused however is that the Version field is only 4 bits wide. IHL is also only 4 bits wide. How do I fit the values of both of those fields to be represented as a byte? Do I need to do bitshifting? E.g. Version = 4, IHL = 5. I would need to create a byte that would equal 0100 0101 = 45h or 69

Bit Hack - Round off to multiple of 8

穿精又带淫゛_ 提交于 2019-12-02 18:53:34
can anyone please explain how this works (asz + 7) & ~7; It rounds off asz to the next higher multiple of 8. It is easy to see that ~7 produces 11111000 (8bit representation) and hence switches off the last 3 bits ,thus any number which is produced is a multiple of 8. My question is how does adding asz to 7 before masking [edit] produce the next higher[end edit] multiple of 8 ? I tried writing it down on paper like : 1 + 7 = 8 = 1|000 (& ~7) -> 1000 2 + 7 = 9 = 1|001 (& ~7) -> 1000 3 + 7 = 10 = 1|010 (& ~7) -> 1000 4 + 7 = 11 = 1|011 (& ~7) -> 1000 5 + 7 = 12 = 1|100 (& ~7) -> 1000 6 + 7 = 13

XOR Operation Intuition

拜拜、爱过 提交于 2019-12-02 18:49:49
I recently came across this question on Leetcode and figured out a solution that I need some clarification with: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? class Solution { public: int singleNumber(vector<int>& nums) { int result = 0; for(auto & c : nums) { result ^= c; } return result; } }; First of all, what sorts of keywords should I be paying attention to in order to figure out that I should be using an XOR operation for this

Number of pairs with constant difference and bitwise AND zero

放肆的年华 提交于 2019-12-02 18:24:28
问题 How to find the number of pairs whose difference is a given constant and their bitwise AND is zero? Basically, all (x,y) such that x-y = k; where k is a given constant and x&y = 0; 回答1: An interesting problem. Let k n-1 ...k 1 k 0 be the the binary representation of k . Let l be the index of the smallest i such that k i =1 We can remark that a potential pair of solutions x and y must have all their bits i , i<l at zero. Otherwise the only way to have a difference x-y with its i th bit unset

Algorith problem Decode Hex to set output

♀尐吖头ヾ 提交于 2019-12-02 18:22:48
问题 I got an algorithm that I need to solve. Unfortunately, I can't even find a clue about a solution. Please help me to understand how to solve this problem. The problem An imaginary pen input device sends hex data to the device to display a line with color. Examples Draw simple Green Line Set color to green, draw a line from (0,0) to (4000, 4000). Filled circle in this diagram indicates a pen down position, empty circle indicates pen up position. Input Data:

How do I flip a bit in SQL Server?

血红的双手。 提交于 2019-12-02 17:48:50
I'm trying to perform a bitwise NOT in SQL Server. I'd like to do something like this: update foo set Sync = NOT @IsNew Note: I started writing this and found out the answer to my own question before I finished. I still wanted to share with the community, since this piece of documentation was lacking on MSDN (until I added it to the Community Content there, too). Yes, the ~ operator will work. update foo set Sync = ~@IsNew Bitwise NOT: ~ Bitwise AND: & Bitwise OR: | Bitwise XOR: ^ Blorgbeard Lacking on MSDN? http://msdn.microsoft.com/en-us/library/ms173468(SQL.90).aspx ~: Performs a bitwise

Optimize me! (C, performance) — followup to bit-twiddling question

♀尐吖头ヾ 提交于 2019-12-02 17:31:50
Thanks to some very helpful stackOverflow users at Bit twiddling: which bit is set? , I have constructed my function (posted at the end of the question). Any suggestions -- even small suggestions -- would be appreciated. Hopefully it will make my code better, but at the least it should teach me something. :) Overview This function will be called at least 10 13 times, and possibly as often as 10 15 . That is, this code will run for months in all likelihood, so any performance tips would be helpful. This function accounts for 72-77% of the program's time, based on profiling and about a dozen

How to find the position of the only-set-bit in a 64-bit value using bit manipulation efficiently?

二次信任 提交于 2019-12-02 17:26:59
Just say I have a value of type uint64_t seen as sequence of octets (1 octet = 8-bit). The uint64_t value is known containing only one set bit at a MSB position. Thus, the uint64_t value can be in one of the following binary representations: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 10000000 pos = 7 00000000 00000000 00000000 00000000 00000000 00000000 10000000 00000000 pos = 15 00000000 00000000 00000000 00000000 00000000 10000000 00000000 00000000 pos = 23 00000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000 pos = 31 00000000 00000000 00000000 10000000

Manipulate alpha bytes of Java/Android color int

余生长醉 提交于 2019-12-02 17:22:58
If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this: int myOpaqueColor = 0xFFFFFF; float factor = 0; int myTransparentColor = operationThatChangesAlphaBytes(myOpaqueColor, factor); //myTransparentColor should now = 0x00FFFFFF; Ideally, it would be nice to multiply those first bytes by whatever factor is, rather than just set the bytes to a static value. majormajors Check out the Color class. Your code would look a bit something like this. int color =