matlab - How to do bitwise operation decently? -
i'm doing analysis on binary data. suppose have 2 uint8 data values:
a = uint8(0xab); b = uint8(0xcd);
i want take lower 2 bits a
, , whole content b
, make 10 bit value. in c-style, should like:
(a[2:1] << 8) | b
i tried bitget
:
bitget(a,2:-1:1)
but gave me separate [1, 1]
logical type values, not scalar, , cannot used in bitshift
operation later.
my current solution is:
make
a|b
(a
orb
):temp1 = bitor(bitshift(uint16(a), 8), uint16(b));
left shift 6 bits rid of higher 6 bits
a
:temp2 = bitshift(temp1, 6);
right shift 6 bits rid of lower zeros previous result:
temp3 = bitshift(temp2, -6);
putting these on 1 line:
result = bitshift(bitshift(bitor(bitshift(uint16(a), 8), uint16(b)), 6), -6);
this doesn't seem efficient, right? want (a[2:1] << 8) | b
, , takes long expression value.
please let me know if there's well-known solution problem.
that or
equivalent addition when dealing integers
result = bitshift(bi2de(bitget(a,1:2)),8) + b;
e.g
a = 01010111 b = 10010010 result = 00000011 100010010 = a[2]*2^9 + a[1]*2^8 + b
an alternative method be
result = mod(a,2^x)*2^y + b;
where x
number of bits want extract a
, y
number of bits of a
, b
, in case:
result = mod(a,4)*256 + b;
an alternative solution close c
solution:
result = bitor(bitshift(bitand(a,3), 8), b);
Comments
Post a Comment