Ask Your Question

I should store binary values in an array

asked 2021-01-18 04:29:54 -0600

Rama gravatar image

updated 2021-01-18 08:46:33 -0600

slelievre gravatar image

Given a Sage integer, I wish to obtain the list of its binary digits.

How can I get that?

So far, I found the binary method of Sage integers.

Given an integer, that method returns its binary expansion as a string.

For example:

sage: a = 8
sage: a.binary()

Thanks for the response.

Here is a follow-up question.

x = 3
z = IncreasingArrays()
z = x.binary() 

for i in range(x):
    if z[i] == 1:
        print('  ', 2^i)  # Not printing here

I want to print if binary value is 1. And raise it to power 2.

The for loop isn't printing print statement also. Please help me with these for loops and also how to store values in an array.

In C I go:

int a[6];
for(i = 2 ; i <= 5 ; i++) { a[i] = 1 ; }

Here like this in C, array values are set.

How to do the same in Sage please help.

edit retag flag offensive close merge delete


Ask Sage is a questions and answers website.

It stores questions and answers for future reference.

Curious people can learn by reading questions and answers.

People with questions can search the web and find previously asked similar questions.

Once a question has been answered, please do not erase the question or replace it with a different one.

Otherwise it becomes impossible to make sense of anything here.

If an answer solves your problem, accept it to mark the question as solved.

If you have more questions, ask them as separate questions.

slelievre gravatar imageslelievre ( 2021-01-18 08:52:11 -0600 )edit

Thanks please if you understand the question please answer it.

Rama gravatar imageRama ( 2021-01-18 09:19:48 -0600 )edit

Why not open a new question:

  • click the "Edit" button under the question
  • copy the "follow-up" part
  • open a new question
  • paste the "follow-up" part there
  • remove it from here

The new question can say "this is a follow up to Ask Sage question 55323" and you can add a comment here saying "See a follow-up question at Ask Sage question 55xxx".

slelievre gravatar imageslelievre ( 2021-01-18 10:25:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2021-01-18 04:52:05 -0600

tmonteil gravatar image

updated 2021-01-18 05:28:57 -0600

slelievre gravatar image

I am not completely sure about your question (please provide more details if i do not understand correctly), but you can transform the string '1000' into the list of its letters as follows:

sage: list(a.binary())
['1', '0', '0', '0']

Note however that each entry is a string, not an integer. If you want a list of integers, you can transform each letter into an element of ZZ:

sage: [ZZ(i) for i in a.binary()]
[1, 0, 0, 0]

The bits and digits methods can give these bits (or "binary digits") directly as integers, ordered the other way around:

sage: a.bits()
[0, 0, 0, 1]

sage: a.digits(base=2)
[0, 0, 0, 1]
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


Asked: 2021-01-18 04:29:54 -0600

Seen: 39 times

Last updated: Jan 18