bit-manipulation

BitCount Method

送分小仙女□ 提交于 2019-12-22 11:29:58
问题 Can anyone Explain this bitcount method. public static int bitCount(int i) { // Hacker's Delight, Figure 5-2 i -= (i >> 1) & 0x55555555; i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = ((i >> 4) + i) & 0x0F0F0F0F; i += i >> 8; i += i >> 16; return i & 0x0000003F; } 回答1: It's based on three observations, The bitcount of a single bit is that bit itself. The bitcount of the concatenation of two bitstrings is the sum of their bitcounts. The bitcount of any string takes no more bits than that

Setting a bit of an unsigned char with the another bit of another unsigned char without conditional

寵の児 提交于 2019-12-22 11:18:23
问题 I use bitwise to turn bits on and off this way: unsigned char myChar = ...some value myChar |= 0x01 << N // turn on the N-th bit myChar &= ~(0x01 << N) //turn off the N-th bit Now, suppose the value of N is know but the set/unset operation depends on the value of a bit of another unsigned char. Since now, I'm doing this way: if ((otherChar & (0x01 << M)) != 0) { //M-th bit of otherChar is 1 myChar |= 0x01 << N; }else { myChar &= ~(0x01 << N); } This should be a sort of "moving bit" operation

Persisting a set of Days of the Week

 ̄綄美尐妖づ 提交于 2019-12-22 10:05:49
问题 I'm looking for an efficient way to persist a set of Days of the Week. In other words, a user will select any number of days from a list of weekdays. The specific date doesn't matter, just the day of the week. I need to store this set of days in a hibernate entity. I could store one of these: Set<DayOfWeek> Set<Integer> But I'm thinking it could be simpler than that. What if I just use a single int to store the data and used bitwise operations. For instance: Sun = 1 Mon = 2 Tue = 4 Wed = 8

Bejeweled bit board applying gravity

社会主义新天地 提交于 2019-12-22 09:47:22
问题 I am attempting to make a Bejeweled cascade simulator with bit boards. So far, I have been able to detect and remove the matches, but now I need to have the jewels fall down. My state is represented by a list of bit boards, one for each type of jewel. I have a mask of all the jewels that are removed. Is it possible to use some bitwise magic to do this? Example of two initial bit boards (let's just assume that there are only two types of jewels and that it's a 4x4 board instead of 8x8). The

Java: Bitwise operation for flag checking

别来无恙 提交于 2019-12-22 09:22:08
问题 I'm trying to check whether or not a number has the second bit flag (ie 0000 0010). My code is as follows: int flags = Integer.parseInt(fields[1]); String strflags = Integer.toBinaryString(flags); flags = Integer.parseInt(strflags); int secondBitTest = Integer.parseInt("00000010", 2); if((flags & secondBitTest) == 2) { System.out.println("YES"); } However I think I might be doing this wrong, since when I try to input 147 nothing is returned. 回答1: You can check if any bit is set using this

Byte shift inverse operation

▼魔方 西西 提交于 2019-12-22 08:54:33
问题 I have this code byte[] b = new byte[]{-33,-4,20,30}; System.err.println(Arrays.toString(b)); int x = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3]; b = new byte[]{(byte)(x >> 24), (byte)(x >> 16), (byte)(x >> 8), (byte)(x)}; System.err.println(Arrays.toString(b)); Output: [-33, -4, 20, 30] [-34, -4, 20, 30] I cannot figure out why this operations are not invers. 回答1: Your problem is unwanted sign extension. Specifically, b[1] is -4 , or 0xfc which is sign-extended to 0xfffffffc then left

Byte shift inverse operation

喜欢而已 提交于 2019-12-22 08:54:22
问题 I have this code byte[] b = new byte[]{-33,-4,20,30}; System.err.println(Arrays.toString(b)); int x = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3]; b = new byte[]{(byte)(x >> 24), (byte)(x >> 16), (byte)(x >> 8), (byte)(x)}; System.err.println(Arrays.toString(b)); Output: [-33, -4, 20, 30] [-34, -4, 20, 30] I cannot figure out why this operations are not invers. 回答1: Your problem is unwanted sign extension. Specifically, b[1] is -4 , or 0xfc which is sign-extended to 0xfffffffc then left

Increase a double to the next closest value?

假如想象 提交于 2019-12-22 08:53:28
问题 This isn't a question for a real-life project; I'm only curious. We can increase an int using the increment operator ( i++ ). You can define this operation as: This increases the variable with the closest value to i . Which is in this case simply +1. But I was thinking of defining the number of double values available in a specific range according the IEEE 754-2008 system. I would be able to set up a graph which demonstrates these amounts in some ranges and see how it is decreasing. I guess

What are good methods for hashing bits in an Int32 or UInt32?

痴心易碎 提交于 2019-12-22 08:24:44
问题 I have an implementation of a pseudo random number generator, specifically of George Marsaglia's XOR-Shift RNG. My implementation is here: FastRandom.cs It turns out that the first random sample is very closely correlated with the seed, which is fairly obvious if you take a look at the Reinitialise(int seed) method. This is bad. My proposed solution is to mix up the bits of the seed as follows: _x = (uint)( (seed * 2147483647) ^ ((seed << 16 | seed >> 48) * 28111) ^ ((seed << 32 | seed >> 32)

Calculate the smallest integer with k bits set that is greater than another integer x?

半城伤御伤魂 提交于 2019-12-22 08:16:28
问题 I want to calculate the smallest integer with exactly k bits set, that is greater than another integer x . For example if x = 1001010 then for k=2 , the answer should be 1010000 for k=4 , the answer should be 1001011 and for k=5 the answer is 1001111 I think that one would need to set at least as many bits as the leftmost bits set in the integer x , and then choose between setting the MSB-side bit adjacent to the next leftmost set bit in x , or setting the next leftmost set bit and then look