how to convert a decimal number within (0, 255) to a 8-bit binary number?

5 ビュー (過去 30 日間)
Yuzhen Lu
Yuzhen Lu 2016 年 10 月 9 日
回答済み: Walter Roberson 2016 年 10 月 9 日
Assume a decimal number 100, it should be converted to 01100100 rather than 01100100 by dec2bin.m.
Is there any efficient way to do that, except bitget(100,[8:-1:1]) that seems a little slow in computation?

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 10 月 9 日
Repeat:
if the current value is odd, emit a 1, otherwise emit a 0
replace the current value with the current value divided by 2, taking the floor() rather than permitting rounding (caution: uint8 rounds)
if the new current value is 0, stop, otherwise repeat

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by