dec2bin function provides wrong answer

For example, when I type dec2bin(8), I expect the answer is 100b, instead the returned value in command window is actually 1000

2 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2022 年 10 月 27 日
Why 100b? Its correct
dec2bin(7)
ans = '111'
dec2bin(8)
ans = '1000'
dec2bin(15)
ans = '1111'
dec2bin(20)
ans = '10100'
Xin
Xin 2022 年 10 月 27 日
Ah! Forgot the last bit. Silly me...
Thank you for the clarification!

サインインしてコメントする。

 採用された回答

John D'Errico
John D'Errico 2022 年 10 月 27 日
編集済み: John D'Errico 2022 年 10 月 27 日

0 投票

So the binary representation of 8 is not 1000?
Let me see, 8 = 1*2^3 + 0*2^2 + 0*2^1 + 0*2^0.
I think you are mistaken.
dec2bin(8)
ans = '1000'
1*2^3 + 0*2^2 + 0*2^1 + 0*2^0
ans = 8
In fact, the number 100 in binary is the decimal 4.
1*2^2 + 0*2^1 + 0*2^0
ans = 4
Maybe you forgot about that least order (units) bit. Note that if binary numbers did not have that least order unit bit, then a binary number could never represent any odd number. And that in itself would be quite an "odd" thing.

1 件のコメント

Xin
Xin 2022 年 10 月 27 日
Yes, indeed. Forgot about the last bit. Thank you so much for your help with this!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

質問済み:

Xin
2022 年 10 月 27 日

コメント済み:

Xin
2022 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by