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
40
votes
7 answers

use of the bitwise operators to pack multiple values in one int

Low level bit manipulation has never been my strong point. I will appreciate some help in understanding the following use case of bitwise operators.Consider... int age, gender, height, packed_info; . . . // Assign values // Pack as AAAAAAA G…
maxpayne
  • 2,479
  • 4
  • 28
  • 28
30
votes
8 answers

How to create a byte out of 8 bool values (and vice versa)?

I have 8 bool variables, and I want to "merge" them into a byte. Is there an easy/preferred method to do this? How about the other way around, decoding a byte into 8 separate boolean values? I come in assuming it's not an unreasonable question, but…
xcel
  • 377
  • 1
  • 3
  • 6
17
votes
9 answers

C++ Data Member Alignment and Array Packing

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,…
Adam Holmberg
  • 7,245
  • 3
  • 30
  • 53
12
votes
7 answers

Bit packing of array of integers

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…
pajton
  • 15,828
  • 8
  • 54
  • 65
11
votes
2 answers

Using reflection to determine how a .Net type is layed out in memory

I'm experimenting with optimizing parser combinators in C#. One possible optimization, when the serialized format matches the in-memory format, is to just do an (unsafe) memcpy of the data to be parsed over an instance or even many instances of the…
Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
8
votes
1 answer

Packing two shorts into one int, dealing with negative and positive

I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and…
Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
8
votes
5 answers

What is VC++ doing when packing bitfields?

To clarify my question, let's start off with an example program: #include #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 …
Rooke
  • 2,013
  • 3
  • 22
  • 34
8
votes
3 answers

Find a NxM grid that contains all possible 3x3 bit patterns

Update: This is called a de Brujin torus, but I still need to figure out a simple algoritm in C#. http://en.wikipedia.org/wiki/De_Bruijn_torus http://datagenetics.com/blog/october22013/index.html I need to combine all values of a 3x3 bit grid as…
Rick Love
  • 12,519
  • 4
  • 28
  • 27
6
votes
2 answers

size and alignment of int bitfields

A struct with bitfields, even when "packed", seems to treat a bitfield's size (and alignment, too?) based on the specified int type. Could someone point to a C++ rule that defines that behavior? I tried with a dozen of compilers and architectures…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
6
votes
4 answers

How to pack arbitrary bit sequence in Python?

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,…
kjo
  • 33,683
  • 52
  • 148
  • 265
5
votes
2 answers

Managing bit packed data using C#

I'm working on a TCP based application that processes bitpacked messages, meaning: The messages transmitted/received are not byte aligned. For instance 3 bits represent field 1, where 19 bits may represent field 2. My question is, does anyone…
nathan
  • 5,513
  • 4
  • 35
  • 47
4
votes
2 answers

Bit packing performance in Julia vs Python

This question (Parallelize code which is doing bit wise operation) got a very good answer and extremely efficient code that I could only match with C code. That prompted me to attempt to at least match the Python code in Julia. The following is the…
Tarik
  • 10,810
  • 2
  • 26
  • 40
4
votes
2 answers

Array inside a bit-packed struct

I'd like to have an array inside of a bit-packed struct. I statically know the size of the array (32), and I'd like each element in the array to be a single bit. For example, I would like to be able to say something like: struct example_s { //…
mhahnenb
  • 78
  • 6
4
votes
5 answers

What is a better method for packing 4 bytes into 3 than this?

I have an array of values all well within the range 0 - 63, and decided I could pack every 4 bytes into 3 because the values only require 6 bits and I could use the extra 2bits to store the first 2 bits of the next value and so on. Having never done…
James Morris
  • 4,867
  • 3
  • 32
  • 51
3
votes
0 answers

C# source generators: Generate different code for calls to the same method

I'll introduce my question using a concrete example. The actual question is at the bottom of this text. Introduction I'd like to extract some unaligned data from byte arrays where given are the start bit position the number of bits whether the data…
sonntam
  • 358
  • 1
  • 2
  • 14
1
2 3 4 5