Ask Your Question

binary value

asked 2018-04-06 19:16:46 +0200

santoshi gravatar image

how to obtain binary of following an the object is very large 189866136719308462018271159242437168532L

edit retag flag offensive close merge delete


(I tried repeated times to post the answer, got internal error, that i should report...)

bin( 189866136719308462018271159242437168532L )
dan_fulea gravatar imagedan_fulea ( 2018-04-07 15:54:04 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-04-06 19:53:23 +0200

slelievre gravatar image

One 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)

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]
edit flag offensive delete link more

answered 2018-04-07 16:12:02 +0200

dan_fulea gravatar image

updated 2018-04-07 16:17:28 +0200

Alternatively, if we want the binary representation as a p h o n e number:

sage: a = 189866136719308462018271159242437168532L
sage: bin(a)

sage: type(bin(a))
<type 'str'>

This is similar to

sage: hex(a)
sage: oct(a)

We can check that this matches the bits solution from the answer of slelievre:

sage: ZZ(a).bits() == [ int(s) for s in bin(a)[:1:-1] ]

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.)

edit flag offensive delete link more


@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.

slelievre gravatar imageslelievre ( 2018-04-07 21:14:15 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-04-06 19:16:46 +0200

Seen: 1,995 times

Last updated: Apr 07 '18