For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
bin
def bin(num: Scalar, /, *, prefix: StaticString = "0b") -> String
Returns the binary string representation of an integer.
The string is prefixed with "0b" by default. Negative values start
with a minus sign, like -0b1111011.
For example:
print(bin(123)) # 0b1111011
print(bin(-123)) # -0b1111011
print(bin(123, prefix="")) # 1111011
Args:
- num (
Scalar): The integer value to format. - prefix (
StaticString): The prefix prepended to the digits (defaults to"0b").
Returns:
String: The binary string representation of the integer.
def bin(b: Scalar[DType.bool], /, *, prefix: StaticString = "0b") -> String
Returns the binary string representation of a bool value.
The bool is formatted as an integer, so the result is 0b0 or 0b1
with the default prefix.
Args:
- b (
Scalar[DType.bool]): The bool value to format. - prefix (
StaticString): The prefix prepended to the digits (defaults to"0b").
Returns:
String: The binary string representation of the bool.
def bin[T: Intable, //](num: T, /, *, prefix: StaticString = "0b") -> String
Returns the binary string representation of a value.
The value is converted to Int and formatted in base 2, prefixed with
"0b" by default.
Parameters:
- T (
Intable): The type of the value. Must conform toIntable.
Args:
- num (
T): The value to format. - prefix (
StaticString): The prefix prepended to the digits (defaults to"0b").
Returns:
String: The binary string representation of the value.