Extracting representative bits for an integer and doing the reverse.

I have an integer value and want to extract all its representative bits in two-bit blocks; each block consists of two bits. Reversely, I want to build an integer value from given two-bit blocks. What is the more optimized code performing these tasks?

 Accepted Answer

For Positive integer Values,By the use of bitwise operations, and,or,shift:
% Decomposition:
a=4235; % any Positive Integer <= 65535 : Up to 2byte.
a1=a;
block=[0,0,0,0,0,0,0,0];
for k=1:8
block(k) = bitand(a1, 3);
a1 = bitshift(a1, -2);
end
% Reversely: The Composition
% We have the resulted block.
b=0; % Integer
for k=1:8
b = bitshift(b, +2);
b = bitor(b,block(9-k));
end
comp=isequal(a,b);

More Answers (1)

% Say, for 171 = AB = 10101011.
str = dec2bin(171)
theBlocks = reshape(str, [2 4])'
In the command window:
str =
10101011
theBlocks =
10
10
10
11
To reconstruct from theBlocks:
reconstructedString = reshape(theBlocks', [1 8])
reconstructedInteger = bin2dec(reconstructedString)
In the command window:
reconstructedString =
10101011
reconstructedInteger =
171
Is that what you mean?

8 Comments

OK, Here the blocks are in string type, How I can convert them to integer type. Finally, they should be 0, 1, 2 or 3 values. And the reverse. Regards
subtract '0' (the string which is the character for the digit 0) to convert to integer type.
Multiply the first of each pair by 2 and add the second of each pair, to get 0, 1, 2, or 3.
Please post example data in Matlab syntax. "Blocks" and "string type", "integer type", "0,1,2,3 values" and "reverse" is not exactly clear, such that an answer needs too much guessing.
Exactly, I need to convert any integer value to smaller integer values belonging to the group {0,1,2,3}.
Any integer? What about negative integers?
How about smallNumber = mod(yourBigNumber, 3). That will give 0-3 for any integer. I totally agree with Jan that you're not being clear about what you want, hence my answer now being totally different than what I first gave. Why don't you give a question that can be answered the very first time without us guessing and trying numerous times to help you?
I'm pretty sure that the poster is trying to do base 4 encoding / decoding.
The answer of image analyst was close to what I wanted but it gives the smaller elements in string type: ‘00’,’01’,’10’,’11’. Instead I need these elements as integer: 0, 1, 2, and 3. In other words I want to decompose the integer to smaller representative integers each has two bits, and from these small integers I can reconstruct the original big integer. To Walter Roberson, it is positive integer. Anyway, I’ve written and tested a code doing exactly what I need,it is done using the bitwise operations, and,or,shift:
% Decomposition:
a=4235; % any Integer <= 65535 : Up to 2byte.
a1=a;
block=[0,0,0,0,0,0,0,0];
for k=1:8
block(k) = bitand(a1, 3);
a1 = bitshift(a1, -2);
end
% Reversely: The Composition:
% We have the resulted block.
b=0; % Integer
for k=1:8
b = bitshift(b, +2);
b = bitor(b,block(9-k));
end
comp=isequal(a,b);
Best Regards

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!