ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 27 Sep 2019 16:06:47 -0500sage code for decimal to binary expansionhttps://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/ k=4 then (0100)Fri, 16 Dec 2016 08:36:54 -0600https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/Answer by John Palmieri for <p>k=4 then (0100)</p>
https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?answer=36035#post-id-36035This gives the list of digits rather than a string:
sage: a = 14
sage: a.digits(2)
[0, 1, 1, 1]
Note that these are listed starting with the units digit.Fri, 16 Dec 2016 09:50:34 -0600https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?answer=36035#post-id-36035Comment by John Palmieri for <p>This gives the list of digits rather than a string:</p>
<pre><code>sage: a = 14
sage: a.digits(2)
[0, 1, 1, 1]
</code></pre>
<p>Note that these are listed starting with the units digit.</p>
https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=48088#post-id-48088@VivekGandhi: what is the binary representation for a rational number? Is `a.numerator().binary() + '/' + a.denominator().binary()` what you mean? By the way, I don't think there can be a general answer for all "Sage ring data types," since they won't all have a meaningful binary representation.Fri, 27 Sep 2019 16:06:47 -0500https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=48088#post-id-48088Comment by VivekGandhi for <p>This gives the list of digits rather than a string:</p>
<pre><code>sage: a = 14
sage: a.digits(2)
[0, 1, 1, 1]
</code></pre>
<p>Note that these are listed starting with the units digit.</p>
https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=48080#post-id-48080`b = a.digits(2); b.reverse(); ''.join(map(str,b))` pretty much does the same thing as `a.binary()`. Doing this with other primitive data types can be done using the Python 'struct' package. For example, for floats: `''.join(bin(ord(c)).replace('0b', '').rjust(8, '0') for c in struct.pack('!f', num))`. Anyone has any idea how to print the binary representation for Sage ring data types such as Rationals?Fri, 27 Sep 2019 04:22:52 -0500https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=48080#post-id-48080Comment by kcrisman for <p>This gives the list of digits rather than a string:</p>
<pre><code>sage: a = 14
sage: a.digits(2)
[0, 1, 1, 1]
</code></pre>
<p>Note that these are listed starting with the units digit.</p>
https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=36043#post-id-36043So in either case you have to do something... maybe `b = a.digits(2); b.reverse()` like [this](http://sagecell.sagemath.org/?z=eJxLUrBVMNFLyUzPLCnWMNLk5UrSK0otSy0qTtUAcQCL8gi2&lang=sage)?Sat, 17 Dec 2016 08:14:59 -0600https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?comment=36043#post-id-36043Answer by kcrisman for <p>k=4 then (0100)</p>
https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?answer=36034#post-id-36034Interestingly, this hasn't been properly asked sometimes. [Here is an example](http://sagecell.sagemath.org/?z=eJxLVLBVMDTl5UrUS8rMSyyq1NAEACiZBME=&lang=sage):
sage: a = 15
sage: a.binary()
'1111'Fri, 16 Dec 2016 08:57:34 -0600https://ask.sagemath.org/question/36033/sage-code-for-decimal-to-binary-expansion/?answer=36034#post-id-36034