bit-manipulation

How to get most significant n bits from int in java

二次信任 提交于 2019-12-05 19:22:56
I have an int, and I would like to get the 19 most significant bits in java. I tried all sorts of methods, none of them work. Can someone please help me? From the 32 bit int, you want to keep the 19 most significant, so discard the 13 least; then you shift right by 13 bits, but have to get rid of the possible sign extension, by anding with a 19 bit pattern: (myint >> 13) & 0x7ffff Adding to Bram's answer, you don't even need the AND if you use unsigned shift. myInt >>> 13; will give you the 19MSB (although they're now situated in the lowest bits). Integer is of 4 byte i.e. 4*8 = 32 bits. So if

Persisting a set of Days of the Week

与世无争的帅哥 提交于 2019-12-05 19:21:24
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 Thu = 16 Fri = 32 Sat = 64 So to represent the set of Sun, Mon, Wed, Sat I would persist the integer 75.

Reversing a Number using bitwise shift

China☆狼群 提交于 2019-12-05 19:17:49
I am trying to find a way to reverse a number without Converting it to a string to find the length Reversing the string and parsing it back Running a separate loop to compute the Length i am currently doing it this way public static int getReverse(int num){ int revnum =0; for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){ revnum += num % 10 * Math.pow( 10 , i ); num /= 10; } return revnum; } But I would Like to implement the above 3 conditions. I am looking for a way , possibly using the bit wise shift operators or some other kind of bitwise operation. Is it possible ? If so how

Bit twiddling for checking whether a number is in particular range

蓝咒 提交于 2019-12-05 19:13:58
问题 I found some interesting bit twiddling in "source\common\unicode\utf.h" file of ICU library (International Components for Unicode). The bit twiddling is intended for checking whether a number is in a particular range. // Is a code point in a range of U+d800..U+dbff? #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) I have figured out the magic number (0xfffffc00) come from: MagicNumber = 0xffffffff - (HighBound - LowBound) However, I also found that the formula doesn't apply to every arbitrary

Bejeweled bit board applying gravity

不羁岁月 提交于 2019-12-05 18:57:41
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 first bit is the bottom left, the fourth bit is top left, and the last bit is top right. 0 0 1 1 1 1 0 0

Mysql Bitwise operations and filter

点点圈 提交于 2019-12-05 18:57:12
问题 I try to implement a bitwise filter using MYSQL (with udf if needed) The filter is something like a AND but I want to use the mask to build a new bit string... Let me explain you with a sample : Suppose I have a table with blob storing 8 bit streams: data1: 10110110 data2: 01100010 data3: 00010011 Then I have a mask to apply to get the bits from data when mask value is 1 MASK: 00101011 And so get the following expected results: data1: 1010 data2: 1010 data3: 0011 Is there a way to optimize

Number of bits to represent a number

淺唱寂寞╮ 提交于 2019-12-05 18:55:53
问题 I'm trying to write a function to return the number of bits a positive integer less that the Javascript limit of (2^53)-1 is. However im being hit by precision problems, and want to avoid big integer libraries. Method 1: function bitSize(num) { return Math.floor( Math.log(num) / Math.log(2) ) + 1; } Pass: bitSize( Math.pow(2, 16) -1 ) = 16 Pass: bitSize( Math.pow(2, 16) ) = 17 Fail (Should be 48): bitSize( Math.pow(2, 48) -1 ) = 49 Pass: bitSize( Math.pow(2, 48) ) = 49 Method 2: function

Java: Bitwise operation for flag checking

谁说胖子不能爱 提交于 2019-12-05 18:30:20
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. You can check if any bit is set using this code that I found here . if (x & (1<<n) != 0) { //n-th bit is set } else { //n-th bit is not set } x is the

Gold Rader bit reversal algorithm

时间秒杀一切 提交于 2019-12-05 18:21:07
I am trying to understand this bit reversal algorithm. I found a lot of sources but it doesn't really explain how the pseudo-code works. For example, I found the pseudo-code below from http://www.briangough.com/fftalgorithms.pdf for i = 0 ... n − 2 do k = n/2 if i < j then swap g(i) and g(j) end if while k ≤ j do j ⇐ j − k k ⇐ k/2 end while j ⇐ j + k end for From looking at this pseudo-code, I don't understand why you would do swap g(i) and g(j) when the if statement is true . Also: what does the while loop do? It would be great if someone can explain this pseudo-code to me. below is the c++

Best way to store long binary (up to 512 bit) in C#

只愿长相守 提交于 2019-12-05 16:37:53
I'm trying to figure out the best way to store large binary (more than 96 bit) numbers in C# I'm building application that will automatically allocate workers for shifts. Shifts can be as short as 15 minutes (but this might be even smaller in the future). To avoid double-booking of workers, I plan to have binary map of their daily time: 24 hours separated in equal chunks (15 minutes) and every chunk has a flag (0 for free, 1 for busy) So when we try to give another shift to a worker, we can do binary comparison of workers daily availability with shift's time. Simple and easy to decide. But C#