Ask Your Question
0

sage code for decimal to binary expansion

asked 2016-12-16 08:36:54 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

k=4 then (0100)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-12-16 09:50:34 -0500

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

edit flag offensive delete link more

Comments

So in either case you have to do something... maybe b = a.digits(2); b.reverse() like this?

kcrisman gravatar imagekcrisman ( 2016-12-17 08:14:59 -0500 )edit
1

answered 2016-12-16 08:57:34 -0500

kcrisman gravatar image

Interestingly, this hasn't been properly asked sometimes. Here is an example:

sage: a = 15
sage: a.binary()
'1111'
edit flag offensive delete link more

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

Stats

Asked: 2016-12-16 08:36:54 -0500

Seen: 182 times

Last updated: Dec 16 '16