Extracting representative bits for an integer and doing the reverse.
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 18 Nov. 2012
Bearbeitet: Image Analyst
am 18 Nov. 2012
% 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 Kommentare
Digitalsd
am 19 Nov. 2012
Walter Roberson
am 19 Nov. 2012
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.
Jan
am 19 Nov. 2012
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.
Walter Roberson
am 19 Nov. 2012
Any integer? What about negative integers?
Image Analyst
am 19 Nov. 2012
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?
Walter Roberson
am 19 Nov. 2012
I'm pretty sure that the poster is trying to do base 4 encoding / decoding.
Digitalsd
am 19 Nov. 2012
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!