bit-manipulation

bit twiddling in java - decomposing long into long[] of bitmasks

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 07:46:16
问题 I am decomposing a single long into a long[] of single bit longs by public static int decompose(long[] buffer, long base) { int count = Long.bitCount(base); for (int i=0; i<count; i++) { base ^= (buffer[i] = Long.lowestOneBit(base)); } return count; } but I feel like there might be a faster way to do this, since it seems like there are some repeated steps. For example, counting the bits should already come pretty close to getting all the info needed to populate the result. Any suggestions? I

bit twiddling in java - decomposing long into long[] of bitmasks

喜你入骨 提交于 2019-12-22 07:46:14
问题 I am decomposing a single long into a long[] of single bit longs by public static int decompose(long[] buffer, long base) { int count = Long.bitCount(base); for (int i=0; i<count; i++) { base ^= (buffer[i] = Long.lowestOneBit(base)); } return count; } but I feel like there might be a faster way to do this, since it seems like there are some repeated steps. For example, counting the bits should already come pretty close to getting all the info needed to populate the result. Any suggestions? I

C: Most efficient way to set all bits in a range within a variable

安稳与你 提交于 2019-12-22 06:51:20
问题 Let's take int as an example: int SetBitWithinRange(const unsigned from, const unsigned to) { //To be implemented } SetBitWithinRange is supposed to return an int in which all and only the bits starting at bit from to bit to are set, when from is smaller than to and both are in the range of 0 to 32 . e.g.: int i = SetBitWithinRange(2,4) will result in i having the value of 0b00...01100 回答1: Here are some ways. First, some variants of "set n bits, then shift by from ". I'll answer in C# though

Bit Reversal using bitwise

前提是你 提交于 2019-12-22 06:44:29
问题 I am trying to do bit reversal in a byte. I use the code below static int BitReversal(int n) { int u0 = 0x55555555; // 01010101010101010101010101010101 int u1 = 0x33333333; // 00110011001100110011001100110011 int u2 = 0x0F0F0F0F; // 00001111000011110000111100001111 int u3 = 0x00FF00FF; // 00000000111111110000000011111111 int u4 = 0x0000FFFF; int x, y, z; x = n; y = (x >> 1) & u0; z = (x & u0) << 1; x = y | z; y = (x >> 2) & u1; z = (x & u1) << 2; x = y | z; y = (x >> 4) & u2; z = (x & u2) <<

Are there any well-established/standardized ways to use fixed-width integers in C89?

断了今生、忘了曾经 提交于 2019-12-22 05:46:06
问题 Some background : the header stdint.h is part of the C standard since C99. It includes typedefs that are ensured to be 8, 16, 32, and 64-bit long integers, both signed and unsigned. This header is not part of the C89 standard, though, and I haven't yet found any straightforward way to ensure that my datatypes have a known length. Getting to the actual topic The following code is how SQLite (written in C89) defines 64-bit integers, but I don't find it convincing. That is, I don't think it's

Are bitwise operations still practical?

百般思念 提交于 2019-12-22 05:18:14
问题 Wikipedia, the one true source of knowledge, states: On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication). Is there a practical reason to learn bitwise operation hacks or it is now just something you learn for

How does ConstantTimeByteEq work?

故事扮演 提交于 2019-12-22 05:17:17
问题 In Go's crytography library, I found this function ConstantTimeByteEq. What does it do, how does it work? // ConstantTimeByteEq returns 1 if x == y and 0 otherwise. func ConstantTimeByteEq(x, y uint8) int { z := ^(x ^ y) z &= z >> 4 z &= z >> 2 z &= z >> 1 return int(z) } 回答1: x ^ y is x XOR y , the result has 1 for the bits x and y are different and 0 for the bits they are same: x = 01010011 y = 00010011 x ^ y = 01000000 ^(x ^ y) negates this, i.e., you get 0 for the bits they are different

bitwise indexing in C?

假装没事ソ 提交于 2019-12-22 04:43:09
问题 I'm trying to implement a data compression idea I've had, and since I'm imagining running it against a large corpus of test data, I had thought to code it in C (I mostly have experience in scripting languages like Ruby and Tcl.) Looking through the O'Reilly 'cow' books on C, I realize that I can't simply index the bits of a simple 'char' or 'int' type variable as I'd like to to do bitwise comparisons and operators. Am I correct in this perception? Is it reasonable for me to use an enumerated

Anding with 0xff, clarification needed

北战南征 提交于 2019-12-22 04:35:23
问题 In the following snippet consider replacing line 8 with commented equivalent 1. private static String ipToText(byte[] ip) { 2. StringBuffer result = new StringBuffer(); 3. 4. for (int i = 0; i < ip.length; i++) { 5. if (i > 0) 6. result.append("."); 7. 8. result.append(ip[i]); // compare with result.append(0xff & ip[i]); 9. } 10. 11. return result.toString(); 12. } .equals() test confirms that adding 0xff does not change anything. Is there a reason for this mask to be applied? 回答1: byte in

High Order Bits - Take them and make a uint64_t into a uint8_t

℡╲_俬逩灬. 提交于 2019-12-22 04:12:20
问题 Let's say you have a uint64_t and care only about the high order bit for each byte in your uint64_t. Like so: uint32_t: 0000 ... 1000 0000 1000 0000 1000 0000 1000 0000 ---> 0000 1111 Is there a faster way than: return ( ((x >> 56) & 128)+ ((x >> 49) & 64)+ ((x >> 42) & 32)+ ((x >> 35) & 16)+ ((x >> 28) & 8)+ ((x >> 21) & 4)+ ((x >> 14) & 2)+ ((x >> 7) & 1) ) Aka shifting x, masking, and adding the correct bit for each byte? This will compile to a lot of assembly and I'm looking for a quicker