Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.6K Views
|
3 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Convert a Number to Binary

Posted 6 years ago

I am trying to convert a number to binary without using BaseForm. Can anyone please tell me why this code isn't working?

binarize = 
      a = Input["Enter No."];
    convert = {}
While[a > 0, AppendTo[convert, Mod[a, 2]]],
    a = Floor[Divide[a, 2]]
    If[a == 0, True],
    Break[]
Print[Reverse[convert]]
POSTED BY: Aryan Deshpande
3 Replies
Posted 6 years ago

Hi Aryan,

What is the definition of "without using built-in Wolfram Functions"? What about this

Table[BitGet[a, i], {i, 1, BitLength[a]}]

or this

IntegerDigits[a, 2]
POSTED BY: Rohit Namjoshi

Thanks for the reply! It works! I am trying to convert the numbers without using built-in Wolfram Functions.

POSTED BY: Aryan Deshpande

Hi,

I think that this might work:

a = Input["Enter No."];
convert = {}
While[a > 0, AppendTo[convert, Mod[a, 2]];
 a = Floor[Divide[a, 2]];]
Print[Reverse[convert]]

But why don't you just use something like:

BaseForm[100, 2]

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard