Skip to main content
updated to reverse the result of OP's loop
Source Link
MelaGo
  • 9.1k
  • 1
  • 11
  • 26

Updated to reverse the result of OP's loop

This makes a list (which needs to be reversed):

Q = Quotient[n, 2];
result = {};
While[Q != 0, AppendTo[result, Mod[n, 2]]; Q = Quotient[n, 2]; n = Q]

resultReverse[result]

{1, 0, 1, 0, 1}

If you want a string:

StringJoin[ToString /@ result]Reverse[result]]

"1011""1101"

There's also

BaseForm[13, 2]

enter image description here

IntegerString[13, 2]

"1101"

This makes a list:

Q = Quotient[n, 2];
result = {};
While[Q != 0, AppendTo[result, Mod[n, 2]]; Q = Quotient[n, 2]; n = Q]

result

{1, 0, 1, 1}

If you want a string:

StringJoin[ToString /@ result]

"1011"

There's also

BaseForm[13, 2]

enter image description here

IntegerString[13, 2]

"1101"

Updated to reverse the result of OP's loop

This makes a list (which needs to be reversed):

Q = Quotient[n, 2];
result = {};
While[Q != 0, AppendTo[result, Mod[n, 2]]; Q = Quotient[n, 2]; n = Q]

Reverse[result]

{1, 1, 0, 1}

If you want a string:

StringJoin[ToString /@ Reverse[result]]

"1101"

There's also

BaseForm[13, 2]

enter image description here

IntegerString[13, 2]

"1101"

Source Link
MelaGo
  • 9.1k
  • 1
  • 11
  • 26

This makes a list:

Q = Quotient[n, 2];
result = {};
While[Q != 0, AppendTo[result, Mod[n, 2]]; Q = Quotient[n, 2]; n = Q]

result

{1, 0, 1, 1}

If you want a string:

StringJoin[ToString /@ result]

"1011"

There's also

BaseForm[13, 2]

enter image description here

IntegerString[13, 2]

"1101"