Katkat

Evaluate a Hexadecimal Bitwise AND

Akuna CapitalSoftware Engineer · Technical Screen

Last updated

Tests understanding of bitwise operations, hexadecimal-to-binary mapping, and manual nibble-level alignment without relying on arbitrary-precision integer conversions.

Problem Statement

In low-level systems programming and high-frequency trading protocol parsing, masks and flags are frequently represented as hexadecimal numbers.

Given two hexadecimal string representations a and b, compute their bitwise AND (a & b) and return the result as a normalized hexadecimal string prefixed with 0x.

To simulate processing large streaming byte buffers or fixed-width hardware registers, implement the operation by processing the values nibble-by-nibble (or byte-by-byte) from right to left without converting the entire string into an arbitrary-precision integer.

Function Interface

def bitwise_and_hex(a: str, b: str) -> str:
    pass

Input & Output Format

  • Input: Strings a and b. Each may optionally start with "0x" or "0X" and may contain lowercase or uppercase hexadecimal digits (0-9, a-f, A-F). Strings may also contain leading zeros.
  • Output: A normalized hexadecimal string starting with lowercase "0x" followed by uppercase hexadecimal digits (0-9, A-F). All redundant leading zeros must be stripped. If the final value is zero, return "0x0".

Constraints

  • 1 <= len(a), len(b) <= 100,000
  • a and b contain only valid hexadecimal characters after any optional "0x" or "0X" prefix.
  • Memory complexity must be O(N) where N = max(len(a), len(b)).

Examples

Example 1

Input: a = "0x23", b = "0x34"
Output: "0x20"
Explanation:
- Nibble 1 (least significant): '3' (0011) AND '4' (0100) = 0000 ('0')
- Nibble 2: '2' (0010) AND '3' (0011) = 0010 ('2')
Result: 0x20

Example 2

Input: a = "FA1", b = "0x3b"
Output: "0x21"
Explanation:
Aligning from the right:
- '1' & 'b' (1101): 0001 & 1011 = 0001 ('1')
- 'A' (1010) & '3' (0011): 1010 & 0011 = 0010 ('2')
- 'F' & '0' (unaligned higher nibble): '0'
Result: 0x21

Example 3

Input: a = "0x00FF", b = "0x0000"
Output: "0x0"
Explanation:
The bitwise AND results in 0. Leading zeros are stripped, producing "0x0".

Hints

Hint 1

Each hexadecimal digit corresponds to exactly one 4-bit nibble, meaning bitwise AND can be evaluated one nibble at a time without carrying over to adjacent digits.

Hint 2

Strip any '0x' prefix and align the two strings from right to left; any missing digits in the shorter string can be treated as '0'.

Hint 3

Convert each aligned character to an integer in the range 0–15, compute the bitwise AND of the two integers, convert back to a hex character, and finally trim leading zeros.

Follow-up questions

  • How would you optimize this in C++ to process 64-bit chunks (16 hex digits) at a time using native uint64_t operations or SIMD vector instructions?
  • How does the logic change if the inputs represent signed two's complement hexadecimal values of different lengths?
  • If one of the inputs is a fixed 32-bit mask applied to a continuous stream of hex data, how would you design the buffer processing pipeline?