bit-packing

C# equivalent of python's struct.pack

微笑、不失礼 提交于 2019-12-01 08:28:31
Is there a library for C# that allows similar functionality to python's struct from the standard library ? One can emulate the struct library quite closely with real aligned structs. But I didn't find yet any way to directly control the endianess in C#'s structs (the C#'s structs seems to be geared more towards COM interop, and less toward general purpose binary packing). The closest equivalent would probably be to use BinaryWriter writing into a MemoryStream , or BitConverter for a one-off conversion of a single value into a byte array. If you want to control the endianness of that, you can

C# equivalent of python's struct.pack

蹲街弑〆低调 提交于 2019-12-01 05:46:32
问题 Is there a library for C# that allows similar functionality to python's struct from the standard library? One can emulate the struct library quite closely with real aligned structs. But I didn't find yet any way to directly control the endianess in C#'s structs (the C#'s structs seems to be geared more towards COM interop, and less toward general purpose binary packing). 回答1: The closest equivalent would probably be to use BinaryWriter writing into a MemoryStream, or BitConverter for a one

How to pack arbitrary bit sequence in Python?

淺唱寂寞╮ 提交于 2019-11-30 21:55:35
I want to encode/compress some binary image data as a sequence if bits. (This sequence will, in general, have a length that does not fit neatly in a whole number of standard integer types.) How can I do this without wasting space? (I realize that, unless the sequence of bits has a "nice" length, there will always have to be a small amount [< 1 byte] of leftover space at the very end.) FWIW, I estimate that, at most, 3 bits will be needed per symbol that I want to encode. Does Python have any built-in tools for this kind of work? Scott Griffiths There's nothing very convenient built in but

How to pack arbitrary bit sequence in Python?

走远了吗. 提交于 2019-11-30 17:59:17
问题 I want to encode/compress some binary image data as a sequence if bits. (This sequence will, in general, have a length that does not fit neatly in a whole number of standard integer types.) How can I do this without wasting space? (I realize that, unless the sequence of bits has a "nice" length, there will always have to be a small amount [< 1 byte] of leftover space at the very end.) FWIW, I estimate that, at most, 3 bits will be needed per symbol that I want to encode. Does Python have any

Bit packing of array of integers

久未见 提交于 2019-11-30 07:06:18
I have an array of integers, lets assume they are of type int64_t . Now, I know that only every first n bits of every integer are meaningful (that is, I know that they are limited by some bounds). What is the most efficient way to convert the array in the way that all unnecessary space is removed (i.e. I have the first integer at a[0] , the second one at a[0] + n bits and so on) ? I would like it to be general as much as possible, because n would vary from time to time, though I guess there might be smart optimizations for specific n like powers of 2 or sth. Of course I know that I can just

How to pack bits (efficiently) in CUDA?

痴心易碎 提交于 2019-11-29 12:17:10
I have an array of bytes where each byte is either 0 or 1. Now I want to pack these values into bits, so that 8 original bytes occupy 1 target byte, with original byte 0 going into bit 0, byte 1 into bit 1, etc. So far I have the following in the kernel: const uint16_t tid = threadIdx.x; __shared__ uint8_t packing[cBlockSize]; // ... Computation of the original bytes in packing[tid] __syncthreads(); if ((tid & 4) == 0) { packing[tid] |= packing[tid | 4] << 4; } if ((tid & 6) == 0) { packing[tid] |= packing[tid | 2] << 2; } if ((tid & 7) == 0) { pOutput[(tid + blockDim.x*blockIdx.x)>>3] =

Bit packing of array of integers

冷暖自知 提交于 2019-11-29 08:20:44
问题 I have an array of integers, lets assume they are of type int64_t . Now, I know that only every first n bits of every integer are meaningful (that is, I know that they are limited by some bounds). What is the most efficient way to convert the array in the way that all unnecessary space is removed (i.e. I have the first integer at a[0] , the second one at a[0] + n bits and so on) ? I would like it to be general as much as possible, because n would vary from time to time, though I guess there

C++ Data Member Alignment and Array Packing

北慕城南 提交于 2019-11-28 11:57:35
During a code review I've come across some code that defines a simple structure as follows: class foo { unsigned char a; unsigned char b; unsigned char c; } Elsewhere, an array of these objects is defined: foo listOfFoos[SOME_NUM]; Later, the structures are raw-copied into a buffer: memcpy(pBuff,listOfFoos,3*SOME_NUM); This code relies on the assumptions that: a.) The size of foo is 3, and no padding is applied, and b.) An array of these objects is packed with no padding between them. I've tried it with GNU on two platforms (RedHat 64b, Solaris 9), and it worked on both. Are the assumptions

How to pack bits (efficiently) in CUDA?

限于喜欢 提交于 2019-11-28 05:45:44
问题 I have an array of bytes where each byte is either 0 or 1. Now I want to pack these values into bits, so that 8 original bytes occupy 1 target byte, with original byte 0 going into bit 0, byte 1 into bit 1, etc. So far I have the following in the kernel: const uint16_t tid = threadIdx.x; __shared__ uint8_t packing[cBlockSize]; // ... Computation of the original bytes in packing[tid] __syncthreads(); if ((tid & 4) == 0) { packing[tid] |= packing[tid | 4] << 4; } if ((tid & 6) == 0) { packing

What is VC++ doing when packing bitfields?

别等时光非礼了梦想. 提交于 2019-11-27 15:37:48
To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 6; unsigned int j : 6; unsigned int k : 4; unsigned int l : 15; }; #pragma pack(pop) struct cc c; int main(int argc, char **argv) { printf("%d\n",sizeof(c)); } The output is "8", meaning that the 56 bits (7 bytes) I want to pack are being packed into 8 bytes, seemingly wasting a whole byte. Curious about