Is there a package to perform binary arithmetic like addition, subtraction, and so on? Ex: 1101 + 0001 = 1110.
BUT not as text, like I've typed in here!
Also, is there any other packager rather than xlop for decimal arithmetic operations?
math-mode
Is there a package to perform binary arithmetic like addition, subtraction, and so on? Ex: 1101 + 0001 = 1110.
BUT not as text, like I've typed in here!
Also, is there any other packager rather than xlop for decimal arithmetic operations?
Best Answer
The math functions built into
pgf
can be used:Notes:
An integer with prefix
0b
or0B
is interpreted as a binary number and is automatically converted to base 10. Hence the prefixes in0b#1 + 0b#2
. It should be noted that this feature of treating numbers with a leading0
as an octal number can not be disabled as per Martin Scharrer's answer at Unexpected results from pgfmath functions with numbers with leading 0.\pgfmathbin{x}
converts an integer to a binary representation.\pgfmathprintnumber
is optional and is used to format the number. In this case it automatically detects that the decimal value of the addition is an integer and produces102
instead of102.0
as is the case for the first decimal addition.Code: