![]() I forgot what sub I was on, what I wrote above is how a CPU or microcontroller would do it. However, even reading a variable from RAM to the CPU is slower than division if the variable isn't already in cache, and actually displaying the number on screen, storing it to a file, or transmitting it over a network are far, far slower. ![]() The last digit is 0x32 and you're done.ĭivision and modulo are still a single processor instruction, but because of how they're implemented they are around 10 times slower than multiplication or addition. You now have 2 left, so you divide by 10 and get 0, remainder 2. Your next digit is 1, which becomes 0x31. Next you divide 21 by 10 and get 2, remainder 1. You add 0x30 to get 0x38, the character "8". You divide 218 by 10 and get 21 with remainder 8. You have 218 and are looking for the ones place. Let's say you have a binary number that is 218 and need to show it to the user. Where division and remainder and modulo come into play is converting binary numbers back into decimal strings to display to the user. It does all the math in binary, and addition and multiplication are both very fast, so this is a cheap and easy conversion. It then can subtract 0x30 from each digit and use addition and multiplication by 10 to convert to a single binary number: 9 + 10 * 2 + 10 * 10 * 9 = 129 = 0b10000001. Let's say the user typed 129 and the program wants to convert that to binary. Generally, user input and output happen as strings of characters, and the program will then convert the strings of text to whatever format is needed. When a key is hit on the keyboard, there's a binary constant for each key 0-9 map to binary 0x30-0x39. The computer does all its math in binary (or floating point binary which I'll ignore here).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |