bit-manipulation

Algorithm for bit expansion/duplication?

假装没事ソ 提交于 2020-01-01 09:55:22
问题 Is there an efficient (fast) algorithm that will perform bit expansion/duplication? For example, expand each bit in an 8bit value by 3 (creating a 24bit value): 1101 0101 => 11111100 01110001 11000111 The brute force method that has been proposed is to create a lookup table. In the future, the expansion value may need to be variable. That is, in the above example we are expanding by 3 but may need to expand by some other value(s). This would require multiple lookup tables that I'd like to

Faster way to swap endianness in C# with 32 bit words

人盡茶涼 提交于 2020-01-01 08:45:44
问题 In this question, the following code: public static void Swap(byte[] data) { for (int i = 0; i < data.Length; i += 2) { byte b = data[i]; data[i] = data[i + 1]; data[i + 1] = b; } } was rewritten in unsafe code to improve its performance: public static unsafe void SwapX2(Byte[] Source) { fixed (Byte* pSource = &Source[0]) { Byte* bp = pSource; Byte* bp_stop = bp + Source.Length; while (bp < bp_stop) { *(UInt16*)bp = (UInt16)(*bp << 8 | *(bp + 1)); bp += 2; } } } Assuming that one wanted to do

How is this size alignment working

人走茶凉 提交于 2020-01-01 05:23:05
问题 I am not able to understand the below code with respect to the comment provided. What does this code does, and what would be the equivalent code for 8-aligned ? /* segment size must be 4-aligned */ attr->options.ssize &= ~3; Here, ssize is of unsigned int type. 回答1: Since 4 in binary is 100, any value aligned to 4-byte boundaries (i.e. a multiple of 4) will have the last two bits set to zero. 3 in binary is 11, and ~3 is the bitwise negation of those bits, i.e., ...1111100. Performing a

Hack to convert javascript number to UInt32

那年仲夏 提交于 2020-01-01 04:45:09
问题 Edit: This question is out of date as the Polyfill example has been updated. I'm leaving the question here just for reference. Read the correct answer for useful information on bitwise shift operators. Question: On line 7 in the Polyfill example of the Mozilla Array.prototype.indexOf page they comment this: var length = this.length >>> 0; // Hack to convert object.length to a UInt32 But the bitwise shift specification on Mozilla clearly states that the operator returns a value of the same

Converting a String representation of bits to a byte

三世轮回 提交于 2020-01-01 04:41:06
问题 I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000" (note this is still stored as a String). Encoding g 111 r 10 a 110 p 010 o 011 m 00 Now I need to write this to the file system using a FileOutputStream , the problem I'm having is, how can I convert the string "010100111111011000" to a byte[] / byte s to be written to the file

From hexadecimal to one's complement in Python

旧街凉风 提交于 2020-01-01 04:40:05
问题 Is there an easy way to produce a one's complement in python? For instance, if you take the hex value 0x9E , I need to convert it to 0x61 . I need to swap the binary 1's for 0's and 0's for 1's. It feels like this should be simple. 回答1: Just use the XOR operator ^ against 0xFF: >>> hex(0x9E ^ 0xFF) '0x61' If you need to work with values larger than a byte, you could create the mask from the int.bit_length() method on your value: >>> value = 0x9E >>> mask = (1 << value.bit_length()) - 1 >>>

Conditional Statement using Bitwise operators

六月ゝ 毕业季﹏ 提交于 2019-12-31 13:29:09
问题 So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only "& ^ ~ ! + | >> <<" The expression needs to resemble: a ? b : c So, from what I've been able to tell, the expression needs to look something like: return (a & b) | (~a & c) This works when a = 0, because anding it with b will give zero, and then the or expression will return the right side, (~a & c) which works because ~0 gives all ones

Conditional Statement using Bitwise operators

白昼怎懂夜的黑 提交于 2019-12-31 13:28:15
问题 So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only "& ^ ~ ! + | >> <<" The expression needs to resemble: a ? b : c So, from what I've been able to tell, the expression needs to look something like: return (a & b) | (~a & c) This works when a = 0, because anding it with b will give zero, and then the or expression will return the right side, (~a & c) which works because ~0 gives all ones

Calculating Hamming Weight in O(1) [duplicate]

落爺英雄遲暮 提交于 2019-12-31 09:12:25
问题 This question already has an answer here : Number of bits set in a number [closed] (1 answer) Closed 6 years ago . In binary representation, hamming weight is the number of 1's. I came across web and found an O(1) answer to it: v = v - ((v>>1) & 0x55555555); v = (v & 0x33333333) + ((v>>2) & 0x33333333); int count = ((v + (v>>4) & 0xF0F0F0F) * 0x1010101) >> 24; However I don't quite understand the algorithm and cannot find a description of it anywhere. Can someone please explain it a little

Swap bits in a number in C [duplicate]

一世执手 提交于 2019-12-31 08:39:13
问题 This question already has answers here : Best practices for circular shift (rotate) operations in C++ (16 answers) Closed last year . In a C interview, I was asked to swap the first 4-bits of a number with the last 4 bit. (eg. 1011 1110 should be 1110 1011.) Does anyone have a solution for this? 回答1: If you haven't seen or done much bit twiddling, a good resource to study is: Bit Twiddling Hacks 回答2: unsigned char c; c = ((c & 0xf0) >> 4) | ((c & 0x0f) << 4); 回答3: There is no "correct answer"