decimal to binary manually
3 ビュー (過去 30 日間)
古いコメントを表示
Design a program using a flowchart that prompts the user for a positive integer number smaller than 256 (i.e. [0..255]) and converts it into an 8-bit binary number. The bits are the remainders of the successive divisions of the input number by 2. The first division remainder is the least significant bit (i.e. the right most), and the last division remainder is the most significant bit (i.e. the left most bit). The result should be stored as a string and then printed.
The following is a sample run: Please enter a number between 0 and 255: 83
The binary equivalent of 83 is 01010011
3 件のコメント
Jaya
2022 年 3 月 8 日
編集済み: Jaya
2022 年 3 月 8 日
I didn't run this code but doesn't it work, you mean?
Since you asked a question in this forum I assume you need help on the Matlab version of the code you presented. In that case, please can you try rewriting in Matlab and paste here? But if your final requirement is not a Matlab code but rather help on the technical part then you may consider posting this in some Stack exchange type forums. That would be better for your scenario.
採用された回答
Walter Roberson
2022 年 3 月 8 日
n=n/2;
There is an important difference between that operation in C and MATLAB. When n is a positive integer datatype, then n/2 truncates in C. For example in C integer 5/2 truncates to 2. However in MATLAB, the operation rounds so 5/2 would have an intermediate value of 2.5 and that would round to 3.
The way to handle the situation in MATLAB is to convert the values to double, carry out the floating point operation, then floor() the results
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!