bit-manipulation

How do computers find modulus?

旧街凉风 提交于 2019-12-19 02:16:03
问题 Is there some cool algorithm with bit wise operations? 回答1: Often, the modulus and divide operations on a processor are the same thing. For instance, refer to http://jsimlo.sk/docs/cpu/index.php/div.html . This is the implementation of the divide instruction on Intel processors. 回答2: Most of the time, modulus is just computed by dividing the two numbers. The quotient is stored in one register, and the remainder is stored in the other register. You would go after the remainder. 回答3: If the

How do computers find modulus?

北城余情 提交于 2019-12-19 02:15:24
问题 Is there some cool algorithm with bit wise operations? 回答1: Often, the modulus and divide operations on a processor are the same thing. For instance, refer to http://jsimlo.sk/docs/cpu/index.php/div.html . This is the implementation of the divide instruction on Intel processors. 回答2: Most of the time, modulus is just computed by dividing the two numbers. The quotient is stored in one register, and the remainder is stored in the other register. You would go after the remainder. 回答3: If the

C bit operations / copy one bit from one byte to another byte

一个人想着一个人 提交于 2019-12-18 19:04:45
问题 I know how to set a bit, clear a bit , toggle a bit, and check if a bit is set. But, how I can copy bit, for example nr 7 of byte_1 to bit nr 7 in byte_2 ? It is possible without an if statement (without checking the value of the bit) ? #include <stdio.h> #include <stdint.h> int main(){ int byte_1 = 0b00001111; int byte_2 = 0b01010101; byte_2 = // what's next ? return 0; } 回答1: byte_2 = (byte_2 & 0b01111111) | (byte_1 & 0b10000000); 回答2: You need to first read the bit from byte1 , clear the

how to split 64bit integer to two 32bit integers

非 Y 不嫁゛ 提交于 2019-12-18 16:32:24
问题 I want to split a 64bit integer into two 32bit integers: var bigInt = 0xffffff; var highInt = bigInt >> 8 // get the high bits 0xfff var lowInt = bigInt // cut of the first part (with &)? console.log(highInt); // 0xfff console.log(lowInt); // 0xfff // set them together again var reBigInt = (highInt << 8) + lowInt; Unfortunately neither getting the highInt nor getting the lowInt works... Could somebody give me the answer how I need to use the bitwise operators? regards 回答1: EDIT JavaScript

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

喜夏-厌秋 提交于 2019-12-18 13:32:05
问题 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 ^

Binary Shift Differences between VB.NET and C#

最后都变了- 提交于 2019-12-18 13:15:33
问题 I just found an interesting problem between translating some data: VB.NET: CByte(4) << 8 Returns 4 But C#: (byte)4 << 8 Returns 1024 Namely, why does VB.NET: (CByte(4) << 8).GetType() return type {Name = "Byte" FullName = "System.Byte"} Yet C#: ((byte)4 << 8).GetType() returns type {Name = "Int32" FullName = "System.Int32"} Is there a reason why these two treat the binary shift the same? Following from that, is there any way to make the C# bit shift perform the same as VB.NET (to make VB.NET

In c binary, testing to see if a number is in range

杀马特。学长 韩版系。学妹 提交于 2019-12-18 12:37:47
问题 This is part of a puzzle that I can't figure out. The function takes in three inputs. The first is an int, the second is the lower bound and the third is the upper bound. I need to test to see if that first number is within the lower and upper bound inclusive. If it is in range then return 1, else return 0. The catch is that I can only use ! ~ & ^ | + << >> operations and only a combination of 20 of them.. Also, only int variables may be used, and no if statements, loops or function calls.

Bitwise AND, Bitwise Inclusive OR question, in Java

穿精又带淫゛_ 提交于 2019-12-18 12:28:29
问题 I've a few lines of code within a project, that I can't see the value of... buffer[i] = (currentByte & 0x7F) | (currentByte & 0x80); It reads the filebuffer from a file, stored as bytes, and then transfers then to buffer[i] as shown, but I can't understand what the overall purpose is, any ideas? Thanks 回答1: As the other answers already stated, (currentByte & 0x7F) | (currentByte & 0x80) is equivalent to (currentByte & 0xFF) . The JLS3 15.22.1 says this is promoted to an int : When both

Understanding JavaScript bitwise NOT operator and toString() function

我与影子孤独终老i 提交于 2019-12-18 12:24:56
问题 Thanks to everyone in advance - alert((~1).toString(2)); outputs: -10 But in PHP/Java it outputs 11111111111111111111111111111110 Am I missing something, why does Javascript add a "-" to the output? Thx, Sam 回答1: I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got. The way you calculate the negative of 10 (in base 2)

What is the purpose of “int mask = ~0;”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 12:03:58
问题 I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1 . So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0 ? Can we use -1 instead of ~0 ? 回答1: It's a portable way to set all the binary bits in an integer to 1 bits without having to know how many bits are in the integer on the current architecture. 回答2: C and C++ allow 3 different signed integer formats: sign-magnitude, one