decimal to binary manually
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Faisal Al-Wazir
il 7 Mar 2022
Risposto: Walter Roberson
il 8 Mar 2022
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 Commenti
Jaya
il 8 Mar 2022
Modificato: Jaya
il 8 Mar 2022
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.
Risposta accettata
Walter Roberson
il 8 Mar 2022
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!