bit-manipulation

Variable bit shift

不问归期 提交于 2019-12-04 16:02:30
I'm looking to shift a register bits n times to the left specified with another register. For example, something like this shl eax, ebx The only problem being is that the 2nd operand cannot be a register, it has to be an immediate value. How would I be able to shift a register by a value not known at assemble-time? George Gaál In x86 you can specify a shift count in register cl . So shl eax, cl is valid. mov ecx, ebx shl eax, cl Note that you can only use the cl register, not eax or any other register. Also, shift counts greater than 31 are performed modulo 32. (Not modulo the operand-size, so

Fastest way to get sign in Java?

我是研究僧i 提交于 2019-12-04 15:13:11
问题 I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a = ...; int sign = a >> 31; //0 for pos, 1 for neg sign = ~sign; //1 for pos, 0 for neg sign = sign << 1; //2 for pos, 0 for neg sign -= 1; //-1 for pos, 1 for neg -- perfect. Or more concisely: int sign = (~(a >> 31) << 1) - 1; Does this seem like a good

Python equivalent of C code from Bit Twiddling Hacks?

六眼飞鱼酱① 提交于 2019-12-04 14:06:44
问题 I have a bit counting method that I am trying to make as fast as possible. I want to try the algorithm below from Bit Twiddling Hacks, but I don't know C. What is 'type T' and what is the python equivalent of (T)~(T)0/3? A generalization of the best bit counting method to integers of bit-widths upto 128 (parameterized by type T) is this: v = v - ((v >> 1) & (T)~(T)0/3); // temp v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp c = (T)

Bit banging in ruby

随声附和 提交于 2019-12-04 13:53:28
问题 I want to create a bit, that will contain security permissions for a given user. In c#, I would do this by creating an enumeration, and then I would do some bit banging on the binary value, by anding '&&' to see if it results in a TRUE value. How can I do this best in Ruby? 回答1: If the underlying value is important then you can create a module that you use like an enum module Groups ADMIN = 1 BOSS = 2 CLERK = 4 MEAT = 8 BREAD = 16 CHEESE = 32 end To set permissions just bitwise or them

C - Get a bit from a byte [duplicate]

我与影子孤独终老i 提交于 2019-12-04 13:43:58
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: how to get bit by bit data from a integer value in c? I have a 8-bit byte and I want to get a bit from this byte, like getByte(0b01001100, 3) = 1 回答1: Firstoff, 0b prefix is not C but a GCC extension of C. To get the value of the bit 3 of an uint8_t a , you can use this expression: ((a >> 3) & 0x01) which would be evaluated to 1 if bit 3 is set and 0 if bit 3 is not set. 回答2: First of all C 0b01... doesn't have

How to judge if there is even 1s in a number's binary representation using C? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-04 13:38:37
问题 This question already has answers here : Computing the Parity (2 answers) Closed 6 years ago . There are already questions about counting how many 1 s are there in a number, but this question is about judging whether there is even or odd number of 1s. Any loop or conditional (including switch) statements are not allowed. Also, division, multiplication or modulus operators should be avoided. To be more specific, we may assume that it's a 32-bits unsigned integer. Actually I've got an

Optimize mysql query to use index on a Bitwise where clause

南楼画角 提交于 2019-12-04 13:17:07
问题 I have a query which looks like: select count(*) from m1 WHERE (m1.`resource` & 1472 ); Although I have index on resource it doesn't use it. How can this be optimized to use on bitwise operation. 回答1: I do not believe MySQL can be made to use indexes for bitwise operations. There's some discussion of this in the MySQL Performance forum: http://forums.mysql.com/read.php?24,35318 ("Are index scans possible with bitwise comparison?") where a MySQL employee suggests a solution based on having a

JavaScript: convert a 52-bit integer to 20-bit and 32-bit integers

大城市里の小女人 提交于 2019-12-04 13:08:06
问题 In other languages which can represent 64-bit integers, it is possible to do this very easily... How to store a 64 bit integer in two 32 bit integers and convert back again How to store a 64 bit integer in two 32 bit integers in Ruby // convert 64-bit n to two 32-bit x and y x = (n & 0xFFFFFFFF00000000) >> 32 y = n & 0xFFFFFFFF But JavaScript CAN NOT represent 64-bit integers. It can only represent 52-bit integers without problems. Now that means that it is not possible to convert a 64-bit

Finding all combinations of longs with certain bits set

心不动则不痛 提交于 2019-12-04 13:07:02
This is such an obscure problem that I suspect I'll have to do it at a different level in my code...but hopefully the hive mind that is Stack Overflow can help... I have a long, which if expressed as a binary string will have exactly five bits set. For example, long l = 341; // as a bit string, "101010101" I'm seeking an array containing all ten possible longs which exactly three of those bits set. To continue the example, long[] results = { 101010000, 101000100, 101000001, 100010100, 100010001, 100000101, 1010100, 1010001, 1000101, 10101 } Here's what the appropriate method signature might

How to reverse bits of a byte?

半腔热情 提交于 2019-12-04 12:19:45
问题 For example, in PHP, how would I reverse the bits of the byte 11011111 to 11111011 ? 回答1: The straight forward approach is to perform 8 masks, 8 rotates, and 7 additions: $blah = $blah & 128 >> 7 + $blah & 64 >> 5 + $blah & 32 >> 3 + $blah & 16 >> 1 + $blah & 8 << 1 + $blah & 4 << 3 + $blah & 2 << 5 + $blah & 1 << 7; 回答2: If you have already the bits in the form of a string, use strrev. If not, convert first the byte to its binary representation by using decbin, then reverse using strrev,