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?
採用された回答
その他の回答 (1 件)
Image Analyst
2012 年 11 月 18 日
編集済み: Image Analyst
2012 年 11 月 18 日
% 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 件のコメント
Digitalsd
2012 年 11 月 19 日
Walter Roberson
2012 年 11 月 19 日
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
2012 年 11 月 19 日
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
2012 年 11 月 19 日
Any integer? What about negative integers?
Image Analyst
2012 年 11 月 19 日
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
2012 年 11 月 19 日
I'm pretty sure that the poster is trying to do base 4 encoding / decoding.
Digitalsd
2012 年 11 月 19 日
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!