bit-manipulation

Convert BitArray to a small byte array

隐身守侯 提交于 2019-12-13 18:06:32
问题 I've read the other posts on BitArray conversions and tried several myself but none seem to deliver the results I want. My situation is as such, I have some c# code that controls an LED strip. To issue a single command to the strip I need at most 28 bits 1 bit for selecting between 2 led strips 6 for position (Max 48 addressable leds) 7 for color x3 (0-127 value for color) Suppose I create a BitArray for that structure and as an example we populate it semi-randomly. BitArray ba = new BitArray

Why does matplotlib's Slider only allow a range of 0-7?

孤人 提交于 2019-12-13 17:57:15
问题 I am trying to plot the values of a bitwise circular shift on 1 byte. I'd like to have a slider let me change the original input value. I'm using the slider example on the matplotlib site for reference, but for some reason even though I pass in 0-255 as my slider range when I run my script the range is always 0-7. I'm guessing that somehow the slider is getting locked to my maximum number of x values, but I don't see how. How do I get the slider to let me pick the full 0-255 range? Also,

Bit shifts in C

假装没事ソ 提交于 2019-12-13 17:47:06
问题 If the bit pattern corresponding to a signed integer is shifted to the right then 1 vacant bit will be filled by the sign bit 2 vacant bit will be filled by 0 3 The outcome is implementation dependent 4 none of the above The answer to this question is 3rd option.. Can anybody explain this,, Also give some basic idea, about the theory behind left shift and right shift operators in C programming. E.g. what is filled on the vacant bit when any of the operation is performed. I checked and noticed

Converting bits to bytes in Python

蹲街弑〆低调 提交于 2019-12-13 15:19:47
问题 I am trying to convert a bit string into a byte string, in Python 3.x. In each byte, bits are filled from high order to low order. The last byte is filled with zeros if necessary. The bit string is initially stored as a "collection" of booleans or integers (0 or 1), and I want to return a "collection" of integers in the range 0-255. By collection, I mean a list or a similar object, but not a character string: for example, the function below returns a generator. So far, the fastest I am able

Append 1 to 32 bit numbers to a char buffer

折月煮酒 提交于 2019-12-13 15:01:40
问题 I have a char* buffer, that I want to append integers of various bit sizes (between 1 and 32 ) to. Thus, I need a function: void addBits(char *buffer, int bits_appended_so_far, int object, int object_bit_size); that can move an object of, say, 13 bits to the 470 th bit position of buffer. I could of course shift the bits onto the buffer one by one, but speed is of the essence so it seems like it should be possible to move larger chunks at a time. Is there a standard method to do this? Seems

Bit fields for reading from H/W registers

社会主义新天地 提交于 2019-12-13 14:32:47
问题 I want to read the 2nd, 5th and the 6th bit from a 32-bit register. I have decided to use struct bit fields to store them. Is the following data structure correct? struct readData { int unwanted:1; int reqbit1:1; int unwanted1:2; int reqbit2:2; int unwanted2:26; }; I'm not sure about how the bit fields are created. I'm going to use an API that will copy bytes from the h/w register to this structure directly. In that case, will reqbit1 contain the 2nd bit? As per my understanding, the compiler

Cumulative bitwise operations

狂风中的少年 提交于 2019-12-13 14:26:59
问题 Suppose you have an Array A = [x, y, z, ...] And then you compute a prefix/cumulative BITWISE-OR array P = [x, x | y, x | y | z, ... ] If I want to find the BITWISE-OR of the elements between index 1 and index 6 , how can I do that using this precomputed P array? Is it possible? I know it works in cumulative sums for getting sum in a range, but I am not sure with bit operations. Edit: duplicates ARE allowed in A , so A = [1, 1, 2, 2, 2, 2, 3] is a possibility. 回答1: There is impossible to use

Compact a hex number

时光总嘲笑我的痴心妄想 提交于 2019-12-13 12:25:30
问题 Is there a clever (ie: branchless) way to "compact" a hex number. Basically move all the 0s all to one side? eg: 0x10302040 -> 0x13240000 or 0x10302040 -> 0x00001324 I looked on Bit Twiddling Hacks but didn't see anything. It's for a SSE numerical pivoting algorithm. I need to remove any pivots that become 0. I can use _mm_cmpgt_ps to find good pivots, _mm_movemask_ps to convert that in to a mask, and then bit hacks to get something like the above. The hex value gets munged in to a mask for a

PHP bitwise AND (&) returns negative number

梦想与她 提交于 2019-12-13 10:24:57
问题 The code: echo (5243960811416 & 4040906070209050); Try on http://phptester.net/ and the result (right) will be 20407554584 but on my web hosting it gives -1067281896 . There's any workaround to have 20407554584 ? It's a 32bit limit? Thanks UPDATE /w SOLUTION 回答1: SOLUTION After searchs and searchs I found this and I re-convert in PHP function BitwiseAndLarge($val1, $val2) { $shift = 0; $result = 0; $mask = ~((~0) << 30); // Gives us a bit mask like 01111..1 (30 ones) $divisor = 1 << 30; // To

Rotate a bitmap represented by an array of bytes

依然范特西╮ 提交于 2019-12-13 09:15:37
问题 In an AVR, I'm using an array of eight bytes to store a picture displayed on an 8x8 LED matrix. The picture needs to be rotated from time to time. So, given the picture ┘ defined as: uint8_t rows[8] = { 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b11111111 }; I want to "rotate" this anticlockwise to get ┐ as: uint8_t rows2[8] = { 0b11111111, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001 }; Or this if done clockwise,