How to do binary calculation?

7

For example, $a=0.10101$ in binary representation.

Then how to calculate $a^2$ directly in mathematica?

Is there any function turning binary into decimal? I only found its inverse...

Doris

Posted 2015-07-15T06:41:46.927

Reputation: 172

1Have you seen FromDigits? – Sjoerd C. de Vries – 2015-07-15T06:55:03.363

Related: (5989), (6635), (11065)

– Mr.Wizard – 2015-07-15T09:12:41.330

Answers

12

You can input numbers in any base up to 36 using the notation base^^digits. Digits over 9 are represented using a, b, c, ...

You can print numbers in any base up to 36 using BaseForm.

Thus,

In[1]:= a=2^^0.10101
Out[1]= 0.65625

In[2]:= BaseForm[a^2,2]
Out[2]//BaseForm= Subscript[0.0110111001, 2]

Note that the internal representation of numbers doesn't know or care about bases. These tools are only for inputting or printing numbers, but do not affect how numbers are stored internally. The internal representation is always binary.


IntegerDigits/RealDigits and FromDigits will convert numbers to/from an explicit list of digits. These functions work with any base, not just up to 36.

Szabolcs

Posted 2015-07-15T06:41:46.927

Reputation: 213 047

5It should probably be noted that internal presentation of real numbers in Mathematica is always binary, and this causes rounding errors for ^^ arbitrary-base non-integers with non-power-of-two base. If one wants to perform precise computation on some other base, numbers should be manipulated in their rational forms. – kirma – 2015-07-15T07:10:19.277

1@kirma Thanks for the addendum! – Szabolcs – 2015-07-15T07:11:59.100

Heh, I didn't actually notice your statement about internal format before I wrote my comment. That explains why my comment is... largely, but not entirely redundant. – kirma – 2015-07-15T07:47:36.883

1@kirma It's not redundant at all, as you explained why it is important that the internal representation is always binary. – Szabolcs – 2015-07-15T09:54:18.780

6

You can enter a number in an arbitrary base using base^^digits:

alpha = 2^^0.10101;
BaseForm[alpha, 2]
BaseForm[alpha^2, 2]

Enrique Pérez Herrero

Posted 2015-07-15T06:41:46.927

Reputation: 924