bit-manipulation

Unset the rightmost set bit [duplicate]

橙三吉。 提交于 2019-11-30 08:28:41
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: How do you set, clear and toggle a single bit in C? Removing lowest order bit n is a positive integer. How can its rightmost set bit be unset? Say n = 7 => n = 0111. I want 0110 as the output. Is there any simple bitwise hack to achieve the goal? 回答1: Try n & (n-1) where & is bitwise AND n = 7 n - 1 =6 n & (n-1)=> 0 1 1 1 (7) & 0 1 1 0 (6) --------- 0 1 1 0 (done!) EDIT (in response to the comment given by

In C bits, multiply by 3 and divide by 16

别等时光非礼了梦想. 提交于 2019-11-30 07:45:56
A buddy of mine had these puzzles and this is one that is eluding me. Here is the problem, you are given a number and you want to return that number times 3 and divided by 16 rounding towards 0. Should be easy. The catch? You can only use the ! ~ & ^ | + << >> operators and of them only a combination of 12. int mult(int x){ //some code here... return y; } My attempt at it has been: int hold = x + x + x; int hold1 = 8; hold1 = hold1 & hold; hold1 = hold1 >> 3; hold = hold >> 4; hold = hold + hold1; return hold; But that doesn't seem to be working. I think I have a problem of losing bits but I

Count bits in the number [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 07:42:57
问题 This question already has answers here : Closed 10 years ago . Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? Suppose you have a number. Is there any way to count the bits which equals to 1 in binary representation of that number, not using iteration? I mean, is there any way to do it in constant time using some bitwise operators and masks. I need solution which will work well for both architectures 32 bit and 64 bit. Ah almost forgot, I need it for C language

Converting 8 bit color into RGB value

我与影子孤独终老i 提交于 2019-11-30 07:38:00
问题 I'm implementing global illumination in my game engine with "reflective shadow maps". RSM has i.a. color texture. To save memory. I'm packing 24 bit value into 8 bit value. Ok. I know how to pack it. But how do I unpack it? I had idea to create a 1D texture with 8 bit palette, with 255 different colors. My 8 bit color would be index of pixel in that texture. I'm not sure how to generate this kind of texture. Are there any mathematical ways to convert 8 bit value into rgb? @edit The color is

Count bits used in int

十年热恋 提交于 2019-11-30 07:36:53
If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below: 101 should return 3 000000011 should return 2 11100 should return 5 101010101 should return 9 How can this be obtained the easiest way in Java? I have come up with the following method but can i be done faster: public static int getBitLength(int value) { if (value == 0) { return 0; } int l = 1; if (value >>> 16 > 0) { value >>= 16; l += 16; } if (value >>> 8 > 0) { value >>= 8; l += 8; } if (value >>> 4 > 0) { value >>= 4; l += 4; } if

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

若如初见. 提交于 2019-11-30 07:33:13
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. Range(int x, int lower, int upper){ //... some code here return retVal; } Obviously I understand the

Bitwise AND, Bitwise Inclusive OR question, in Java

两盒软妹~` 提交于 2019-11-30 06:58:35
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 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 operands of an operator &, ^, or | are of a type that is convertible (§5.1.8) to a primitive integral type, binary

Optimization chance for following Bit-Operations?

让人想犯罪 __ 提交于 2019-11-30 06:55:15
Do you think there is room for optimizations in the function haswon (see below)? I recognized that changing the argument type from __int64 to unsigned __int64 made the function faster, thus i thougt maybe there is still a chance for optimization. In more detail: I am writing a connect four game. Recently i used the Profiler Very Sleepy and recognized that the function haswon uses much of the cpu-time. The function uses a bitboard-representation of the connect-four-board for one player. The function itself i found in the sources of the fourstones benchmark. The bitboard representation is

Get an array of the bit positions within a 64-bit integer

巧了我就是萌 提交于 2019-11-30 06:53:30
OK, it may sound a bit complicated, but this is what I'm trying to do : Take e.g. 10101010101 And return { 0, 2, 4, 6, 8, 10 } - an array with all of the positions of bits which are set This is my code : UINT DQBitboard::firstBit(U64 bitboard) { static const int index64[64] = { 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5 }; static const U64 debruijn64 = 0x07EDD5E59A4E28C2ULL; #pragma warning

Understanding JavaScript bitwise NOT operator and toString() function

血红的双手。 提交于 2019-11-30 06:49:25
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 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) using two's complement is that you first invert all of the bits, giving you: 11111111111111111111111111111101