ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 07 Apr 2018 14:14:15 -0500binary valuehttp://ask.sagemath.org/question/41889/binary-value/ how to obtain binary of following an integer.as the object is very large
189866136719308462018271159242437168532LFri, 06 Apr 2018 12:16:46 -0500http://ask.sagemath.org/question/41889/binary-value/Comment by dan_fulea for <p>how to obtain binary of following an integer.as the object is very large
189866136719308462018271159242437168532L</p>
http://ask.sagemath.org/question/41889/binary-value/?comment=41903#post-id-41903(I tried repeated times to post the answer, got internal error, that i should report...)
bin( 189866136719308462018271159242437168532L )Sat, 07 Apr 2018 08:54:04 -0500http://ask.sagemath.org/question/41889/binary-value/?comment=41903#post-id-41903Answer by dan_fulea for <p>how to obtain binary of following an integer.as the object is very large
189866136719308462018271159242437168532L</p>
http://ask.sagemath.org/question/41889/binary-value/?answer=41904#post-id-41904Alternatively, if we want the binary representation as a p h o n e number:
sage: a = 189866136719308462018271159242437168532L
sage: bin(a)
'0b10001110110101101110001101000111101110110011101001111001010101010010100111110111010100011010100111001000110100000000110110010100'
sage: type(bin(a))
<type 'str'>
This is similar to
sage: hex(a)
'0x8ed6e347bb3a795529f751a9c8d00d94L'
sage: oct(a)
'02166556150756635171252247672432471064006624L'
We can check that this matches the `bits` solution from the answer of [slelievre](https://ask.sagemath.org/users/1092/slelievre/):
sage: ZZ(a).bits() == [ int(s) for s in bin(a)[:1:-1] ]
True
Of course.
**Note**: Repeated times i tried to post versions of this answer. (The solution was to take a shower, then i wrote the word phone in an "unexpected" way. It seems that we cannot post the string pho.. number followed by an obvious such number here.)Sat, 07 Apr 2018 09:12:02 -0500http://ask.sagemath.org/question/41889/binary-value/?answer=41904#post-id-41904Comment by slelievre for <p>Alternatively, if we want the binary representation as a p h o n e number:</p>
<pre><code>sage: a = 189866136719308462018271159242437168532L
sage: bin(a)
'0b10001110110101101110001101000111101110110011101001111001010101010010100111110111010100011010100111001000110100000000110110010100'
sage: type(bin(a))
<type 'str'>
</code></pre>
<p>This is similar to</p>
<pre><code>sage: hex(a)
'0x8ed6e347bb3a795529f751a9c8d00d94L'
sage: oct(a)
'02166556150756635171252247672432471064006624L'
</code></pre>
<p>We can check that this matches the <code>bits</code> solution from the answer of <a href="https://ask.sagemath.org/users/1092/slelievre/">slelievre</a>: </p>
<pre><code>sage: ZZ(a).bits() == [ int(s) for s in bin(a)[:1:-1] ]
True
</code></pre>
<p>Of course.</p>
<p><strong>Note</strong>: Repeated times i tried to post versions of this answer. (The solution was to take a shower, then i wrote the word phone in an "unexpected" way. It seems that we cannot post the string pho.. number followed by an obvious such number here.)</p>
http://ask.sagemath.org/question/41889/binary-value/?comment=41906#post-id-41906@dan_fulea -- regarding pho.. ..mber, yes, this is one of the strings that
cause questions or answers to be rejected automatically, as it frequently
signals spam. It would be nice if that could be bypassed for users with
sufficient karma, but I'm not sure the Askbot engine allows that.Sat, 07 Apr 2018 14:14:15 -0500http://ask.sagemath.org/question/41889/binary-value/?comment=41906#post-id-41906Answer by slelievre for <p>how to obtain binary of following an integer.as the object is very large
189866136719308462018271159242437168532L</p>
http://ask.sagemath.org/question/41889/binary-value/?answer=41892#post-id-41892One can convert this "long" to a "Sage integer", and use the method "bits".
sage: a = 189866136719308462018271159242437168532L
sage: b = ZZ(a).bits()
How many bits do we have:
sage: len(b)
128
Show the first eight bits
sage: b[:8]
[0, 0, 1, 0, 1, 0, 0, 1]
and the last eight bits
sage: b[-8:]
[0, 1, 1, 1, 0, 0, 0, 1]
Fri, 06 Apr 2018 12:53:23 -0500http://ask.sagemath.org/question/41889/binary-value/?answer=41892#post-id-41892