bit-manipulation

Using SOLR to calculate “similarity”/“bitcount” between two ulongs

佐手、 提交于 2019-11-30 10:00:42
We have a database of images where I have calculated the PHASH using Dr. Neal Krawetz's method as implemented by David Oftedal . Part of the sample code calculates the difference between these longs is here: ulong hash1 = AverageHash(theImage); ulong hash2 = AverageHash(theOtherImage); uint BitCount(ulong theNumber) { uint count = 0; for (; theNumber > 0; theNumber >>= 8) { count += bitCounts[(theNumber & 0xFF)]; } return count; } Console.WriteLine("Similarity: " + ((64 - BitCount(hash1 ^ hash2)) * 100.0) / 64.0 + "%"); The challenge is that I only know one of these hashes and I want to query

bit shift operation does not return expected result

余生长醉 提交于 2019-11-30 09:48:50
Why does Java return -2147483648 when I bit shift 1 << 63 ? The expected result is 9 223 372 036 854 775 808 , tested with Wolfram Alpha and my calculator. I tested: System.out.print((long)(1 << (63))); Adam Mihalcin There's an important thing to note about the line System.out.print((long)(1 << (63))); You first take (1 << 63) , and then you cast to long. As a result, you are actually left-shifting in integers, so the long cast doesn't have any effect. That's why shifting 63 bits left gives the min integer rather than the min long. But there's another, more important point. Java longs are

Java: Are bitwise OR and AND FASTER than the equivalent logical operators?

余生颓废 提交于 2019-11-30 09:45:44
Cut and dry... while I never have enough logical operations for it to be a performance bottleneck - I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to the same-named logical operators (&& and ||) if possible? Perhaps the question can be prefaced by the fact that I don't know of a library to convert Java to assembly to see the # of operations. Bitwise operators avoid branching instructions, even in Java code execution. As a result you have no expensive branch prediction misses and no jumps at all. From my experience, they can be measurably faster when used in

How do you set, clear and toggle a single bit in Rust?

人盡茶涼 提交于 2019-11-30 09:17:32
How do I set, clear and toggle a bit in Rust? Like many other languages, the bitwise operators & (bitwise AND), | (bitwise OR), ^ (bitwise XOR) exist: fn main() { let mut byte: u8 = 0b0000_0000; byte |= 0b0000_1000; // Set a bit println!("0b{:08b}", byte); byte &= 0b1111_0111; // Unset a bit println!("0b{:08b}", byte); byte ^= 0b0000_1000; // Toggle a bit println!("0b{:08b}", byte); } The main difference from other languages is in bitwise NOT, which uses ! instead of ~ : fn main() { let mut byte: u8 = 0b0000_0000; byte = !byte; // Flip all bits println!("0b{:08b}", byte); } You can also shift

Java and unsigned values

眉间皱痕 提交于 2019-11-30 09:15:24
I'm parsing unsigned bits from a DatagramSocket. I have a total of 24bits (or 3 bytes) coming in - they are: 1 unsigned 8bit integer followed by a 16bit signed integer. But java never stores anything more than a signed byte into a byte/byte array? When java takes in these values, do you lose that last 8th bit? DatagramSocket serverSocket = new DatagramSocket(666); byte[] receiveData = new byte[3]; <--Now at this moment I lost my 8th bit System.out.println("Binary Server Listing on Port: "+port); while (true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

Java how to parse uint8 in java?

眉间皱痕 提交于 2019-11-30 09:12:50
I have a uint8 (unsigned 8 bit integer) coming in from a UDP packet. Java only uses signed primitives. How do I parse this data structure correctly with java? Simply read it as as a byte and then convert to an int. byte in = udppacket.getByte(0); // whatever goes here int uint8 = in & 0xFF; The bitmask is needed, because otherwise, values with bit 8 set to 1 will be converted to a negative int. Example: This: 10000000 Will result in: 11111111111111111111111110000000 So when you afterwards apply the bitmask 0xFF to it, the leading 1's are getting cancelled out. For your information: 0xFF ==

Create a mask that marks the most significant set bit, using only bitwise operators

余生长醉 提交于 2019-11-30 08:59:14
问题 This was part of a larger programming assignment that was due for me last night. Couldn't figure out this problem, but I'm curious as to how it could be solved. The function int greatestBitPos(int x) should return an int mask that marks the position of the most significant bit. If x==0, return 0. No control structures (if, while, ?:) allowed. Example: greatestBitPos(96) = 0x40 Legal operators: ! ~ & ^ | + << >> = This website on bit twiddling is something I used as a starting point,

How do uppercase and lowercase letters differ by only one bit?

半腔热情 提交于 2019-11-30 08:53:53
I have found one example in Data and Communication Networking book written by Behrouza Forouzan regarding upper- and lowercase letters which differ by only one bit in the 7 bit code. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. If we know the code for one case, we can easily find the code for the other by adding or subtracting 32 in decimal, or we can just flip the sixth bit. What does all this mean? I have found myself very confused with all these things. Could someone

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

家住魔仙堡 提交于 2019-11-30 08:44:24
Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest way to find X given Y = 2 X in Python ?"** I am currently trying to optimize a routine ( Rabin-Miller primality test ) that reduces an even number N in the forms 2**s * d . I can get the 2**s part by: two_power_s = N & -N but I can't find a way to extract just " s " with a bitwise operation. Workarounds I am currently testing without too much

finding the first set bit in a binary number [duplicate]

☆樱花仙子☆ 提交于 2019-11-30 08:36:08
问题 This question already has answers here : Efficient bitwise operations for counting bits or find the right|left most ones (3 answers) Position of least significant bit that is set (22 answers) Closed 5 years ago . I need to find the first set bit in a binary number from right to left; I came up with this solution: int cnt=0; while (number& 1 ==0) { cnt++; number>>=1; } Is there a better way of doing it? Some clever bit manipulation technique? 回答1: If you want it to be fast, bitscan instruction