IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
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:

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 to Intable.

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.