Questions tagged [bit-packing]

Use this tag for questions related to bit packing, for example packing bits into integer types.

is used in its general concept, and is usually found in questions using C (or C based languages) or Java.

64 questions
3
votes
2 answers

Pack/unpack array of 64 bools into a uint64

I've come across this answer. But I hit 2 issues, I'm not sure how to adapt it to pack 64 bits (see below), and I can't get my head around unpacking them. Here is what I have for packing: const int i = 1; #define is_bigendian() ((*(char *) &i) ==…
Raphaël Vigée
  • 2,048
  • 14
  • 27
3
votes
2 answers

Python: Fastest way of packing a 2d array of binary values into UINT64 array

I have a 2D UINT8 numpy array of size (149797, 64). Each of the elements are either 0 or 1. I want to pack these binary values in each row into a UINT64 value so that i get a UINT64 array of shape 149797 as a result. I tried the following code using…
anilsathyan7
  • 1,423
  • 17
  • 25
3
votes
2 answers

C# - packing date, month, year

In Programming in the Key of C#, the author gives an example (with source code) on how a date (year, month, day -- in numbers) can be packed in a 32 bit integer. In the example, the author packs the information like so: int iDate = (iYear << 9) |…
3
votes
1 answer

C# equivalent of python's struct.pack

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…
Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169
2
votes
2 answers

Reversing a bit packing algorithm

I'm trying to reverse an algorithm I have that packs a bunch of unsigned shorts into memory. I attempted to reverse it, and I get correct numbers back 50% - 75% of the time, so I'm not sure what I'm doing wrong. This is the algorithm to pack the…
Ash
  • 379
  • 1
  • 3
  • 14
2
votes
1 answer

How do I optimise numpy.packbits with numba?

I'm trying to optimise numpy.packbits: import numpy as np from numba import njit, prange @njit(parallel=True) def _numba_pack(arr, div, su): for i in prange(div): s = 0 for j in range(i*8, i*8+8): s = 2*s + arr[j] …
mathfux
  • 5,759
  • 1
  • 14
  • 34
2
votes
1 answer

Proper way to pack a string in python

What is the correct way to pack a five-byte asci string into python so that it is 8-bytes and little endian? For example, something like: from struct import pack pack('
David542
  • 104,438
  • 178
  • 489
  • 842
2
votes
1 answer

Syntax error in bit packing a structure in C after switching compiler and IDE

Okay, what am I missing.... I wrote the code originally on an IAR IDE/compiler for use on an ARM microprocessor and it worked fine. We are now switching to a different controller with a Tricore microprocessor and are now using code::blocks IDE with…
MidnightRover
  • 197
  • 1
  • 8
2
votes
5 answers

Compressing a 'char' array using bit packing in C

I have a large array (around 1 MB) of type unsigned char (i.e. uint8_t). I know that the bytes in it can have only one of 5 values (i.e. 0, 1, 2, 3, 4). Moreover we do not need to preserve '3's from the input, they can be safely lost when we…
avikpram
  • 47
  • 5
2
votes
2 answers

How to pack bits (efficiently) in CUDA?

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…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
2
votes
1 answer

Aggregating and Dis-Aggregating a power of 2/Table Design Issues

I have some data, and am trying to keep track of various issues with paperwork for work. The data looks something like this: ID | Paperwork 1 | Paperwork 2 | Paperwork 3 1 | No Signature | No Signature | …
user3097236
  • 37
  • 1
  • 8
2
votes
2 answers

small integers in fortran

I need to create a large array of integers in fortran which contains only 1 and -1. Could someone please suggest me how to define such an array that takes minimum possible memory space? Thank you
1
vote
3 answers

Redistribute least significant bits from a 4-byte array to a nibble

I wish to move bits 0,8,16,24 of a 32-bit value to bits 0,1,2,3 respectively. All other bits in the input and output will be zero. Obviously I can do that like this: c = c>>21 + c>>14 + c>>7 + c; c &= 0xF; But is there a faster (fewer instructions)…
Dijkstra
  • 2,490
  • 3
  • 21
  • 35
1
vote
2 answers

How can I optimize this bit-packing function in C?

This code sample takes an arbitrarily large number of bytes as inputs, one at the time, and maps them to a table with 32 values [0,31]. To simplify the example I used mod 32. The >> 3 operation is equivalent to division by 8. In case it is not…
1
vote
2 answers

Performance difference between bitpacking bytes into a u32 vs storing them in a vec?

Intro: I'm curious about the performance difference (both cpu and memory usage) of storing small numbers as bitpacked unsigned integers versus vectors of bytes Example I'll use the example of storing RGBA values. They're 4 Bytes so it is very…
Jam
  • 476
  • 3
  • 9