bit-manipulation

What is the most efficient way in Java to pack bits into byte[] and read it back?

可紊 提交于 2019-12-03 06:55:40
I currently use these two functions to pack and read bits in a byte array. Wondering if anybody has any better ideas or faster ways to do it? Edited the program with a few more optimization and tabled a few calculations. Currently 100mil Put and Get takes about 12 secs instead of 16 secs now. If anybody is using the current code make sure the value passed in to Put is a positive number as it's expecting unsigned numbers coming down. If there is interest I can put up signed and unsigned versions. class BitData { static void Put(byte Data[], final int BitOffset, int NumBits, final int Value) {

Explanation of the safe average of two numbers

梦想的初衷 提交于 2019-12-03 06:36:42
Whenever I need to average two numbers for an algorithm like binary search, I always do something like this: int mid = low + ((high - low) / 2); I recently saw another way to do it in this post , but I don't understand it. It says you can do this in Java: int mid = (low + high) >>> 1; or this in C++: int mid = ((unsigned int)low + (unsigned int)high)) >> 1; The C++ version essentially makes both operands unsigned, so doing a shift results in an arithmetic shift instead of a signed shift. I understand what both these pieces of code are doing, but how does this solve the overflow issue? I

2.9999999999999999 >> .5?

房东的猫 提交于 2019-12-03 06:28:49
I heard that you could right-shift a number by .5 instead of using Math.floor(). I decided to check its limits to make sure that it was a suitable replacement, so I checked the following values and got the following results in Google Chrome: 2.5 >> .5 == 2; 2.9999 >> .5 == 2; 2.999999999999999 >> .5 == 2; // 15 9s 2.9999999999999999 >> .5 == 3; // 16 9s After some fiddling, I found out that the highest possible value of two which, when right-shifted by .5, would yield 2 is 2.9999999999999997779553950749686919152736663818359374999999¯ (with the 9 repeating) in Chrome and Firefox. The number is

XOR Operation Intuition

天大地大妈咪最大 提交于 2019-12-03 06:22:32
问题 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

Finding the next in round-robin scheduling by bit twiddling

删除回忆录丶 提交于 2019-12-03 06:16:47
问题 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

Is it possible to rewrite modulo (2^n - 1) using bitwise and restricted operators

落花浮王杯 提交于 2019-12-03 06:02:24
For unsigned int x, is it possible to calculate x % 255 (or 2^n - 1 in general) using only the following operators (plus no loop, branch or function call)? ! , ~ , & , ^ , | , + , << , >> . Yes, it's possible. For 255, it can be done as follows: unsigned int x = 4023156861; x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); // At this point, x will be in the range: 0 <= x < 256. // If the answer 0, x could potentially be 255 which is not fully reduced. // Here's an ugly way of implementing: if (x == 255) x -= 255; // (See comments for a

Will bit-shift by zero bits work correctly?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:20:44
Say I have a function like this: inline int shift( int what, int bitCount ) { return what >> bitCount; } It will be called from different sites each time bitCount will be non-negative and within the number of bits in int . I'm particularly concerned about call with bitCount equal to zero - will it work correctly then? Also is there a chance that a compiler seeing the whole code of the function when compiling its call site will reduce calls with bitCount equal to zero to a no-op? It is certain that at least one C++ compiler will recognize the situation (when the 0 is known at compile time) and

How to use bitwise operators in if statements?

会有一股神秘感。 提交于 2019-12-03 05:20:22
I want to write something like this: if [[ ( releases["token"] & $MASK ) -eq 1 ]]; then but I get the error that: unexpected token `&', conditional binary operator expected How do I use bitwise operators in if statements? You can use Arithmetic Expansion : (((5&3)==1)) && echo YES || echo NO It will print YES Martin Rüegg Logic vs Syntax .... or: "What's more to say?" At first sight, as pointed out in @chepner's comment , the question might only be one of syntax, which causes the compilation error ( unexpected token '&', conditional binary operator expected ). And in fact, @kev's answer

Bit Hack - Round off to multiple of 8

南笙酒味 提交于 2019-12-03 04:35:17
问题 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

Algorithm for copying N bits at arbitrary position from one int to another

十年热恋 提交于 2019-12-03 04:28:38
问题 An interesting problem I've been pondering the past few days is how to copy one integer's bits into another integer at a given position in the destination integer. So, for example, given the destination integer 0xdeadbeef and the source integer 0xabcd , the idea would be to get a result of 0xabcdbeef (given a destination position of 16 bits) or 0xdeabcdef (given a destination position of 8 bits). With the arbitrary limitation of avoiding conditionals or loops (allowing myself to use just