|
Oktatás * Programozás 2 * Szkriptnyelvek * Adator. prog. Teaching * Prog. for Data Sci. Félévek Linkek * kalendárium |
EnPy /
20120820bDecimal → binary conversion Convert a decimal integer to binary format. Example: 156 in binary format:
quotient remainder
156 / 2 = 78 0
78 / 2 = 39 0
39 / 2 = 19 1
19 / 2 = 9 1
9 / 2 = 4 1
4 / 2 = 2 0
2 / 2 = 1 0
1 / 2 = 0 1
That is: divide the decimal number by 2 until the quotient becomes 0. At that point the algorithm stops. Reading the remainders from bottom to top we get the binary number: 10011100. Exercise: write a program that converts an arbitrary positive integer to binary. Note: do this conversion manually (like above). However, there is a built-in function called |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |