Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Binary literals

Use case

Define ARM64 instruction masks and opcodes clearly.

Explanation

Binary literals (0b...) make bit patterns readable. ' can be used as a separator.

Code

#include <cstdint>
#include <iostream>

int main() {
  // https://developer.arm.com/documentation/ddi0602/2025-12/Base-Instructions/BL--Branch-with-link-?lang=en
  uint32_t blMask = 0b1111'1100'0000'0000'0000'0000'0000'0000;
  uint32_t blOpcode = 0b1001'0100'0000'0000'0000'0000'0000'0000;

  // $ echo "bl #0x40" | llvm-mc -arch=aarch64 -show-encoding
  //    bl      #64                             // encoding: [0x10,0x00,0x00,0x94]
  uint32_t blInsn = 0x94000010;

  bool isBl = (blInsn & blMask) == blOpcode;

  std::cout << "Is BL: " << std::boolalpha << isBl << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++14/build/binary-literals
Is BL: true