Denary to binary · عشري ثنائي
Denary to binary
Go the other way: from a denary number to its binary digits.
Divide by 2 again and again. The remainder n % 2 is the next bit, and n // 2 is what is left to convert. The remainders come out right to left, so put each new bit at the front of the string. 0 needs its own answer, because the loop would never run.
عشري إلى ثنائي
اذهب بالاتجاه المعاكس: من رقم عشري إلى أجزائه الثنائية.
اقسم على 2 مراراً وتكراراً. الباقي n % 2 هو البت التالي، وn // 2 هو المتبقي للتحويل. تظهر البواقي من اليمين إلى اليسار، لذا ضع كل بت جديد في بداية السلسلة. 0 يحتاج إجابته الخاصة، لأن الحلقة لن تعمل أبداً.
Write to_binary(n) that returns the binary digits of a whole number n (0 or more) as a string, without using bin(). 11 gives "1011" and 0 gives "0". · اكتب to_binary(n) ترسل الأرقام الثنائية للعدد الصحيح n (0 أو أكثر) كسلسلة نصية، دون استخدام bin(). 11 يعطي "1011" و 0 يعطي "0".
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.