Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient bit shuffling of vector of binary numbers

I have recorded data containing a vector of bit-sequences, which I would like to re-arrange efficiently. One value in the vector of data could look like this:

bit0, bit1, bit2, ... bit7

I would like to re-arrange this bit-sequence into this order:

bit0, bit7, bit1, bit6, bit2, bit5, bit3, bit4

If I had only one value this would work nicely via:

sum(uint32(bitset(0,1:8,bitget(uint32(X), [1 8 2 7 3 6 4 5]))))

Unfortunately bitset and bitget are not capable of handling vectors of bit-sequences. Since I have a fairly large dataset I am interested in efficient solutions.

Any help would be appreciated, thanks!

like image 564
SCBuergel Avatar asked Sep 28 '22 03:09

SCBuergel


2 Answers

dec2bin and bin2dec can process vectors, you can input all numbers at once and permute the matrix:

input=1:23;
pattern = [1 8 2 7 3 6 4 5];
bit=dec2bin(input(:),numel(pattern));
if size(bit,2)>numel(pattern)
    warning('input numbers to large for pattern, leading bits will be cut off')
end
output=bin2dec(bit(:,pattern));

if available, I would use de2bi and bi2de instead.

like image 181
Daniel Avatar answered Oct 13 '22 01:10

Daniel


I don't know if I may get the question wrong, but isn't it just solvable by indexing wrapped into cellfun?

%// example data
BIN{1} = dec2bin(84,8)
BIN{2} = dec2bin(42,8)

%// pattern and reordering
pattern = [1 8 2 7 3 6 4 5];
output = cellfun(@(x) x(pattern), BIN, 'uni', 0)

Or what is the format of you input and desired output?


BIN = 

    '01010100'    '00101010'


output = 

    '00100110'    '00011001'
like image 33
Robert Seifert Avatar answered Oct 12 '22 23:10

Robert Seifert