Apply a named bit manipulation operation to an 8-bit unsigned integer

Quick Overview

Given an 8-bit unsigned integer and an operation name, return the result of setting, clearing, toggling or reading one bit, checking parity, counting set bits, reversing all 8 bits or swapping the two nibbles. It tests masks, shifts, XOR and fixed-width bit reversal with leading zeros.

Apply a named bit manipulation operation to an 8-bit unsigned integer

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an 8-bit unsigned integer `n` and a string `operation`, perform the specified bit manipulation operation on `n` and return the result as an integer. The supported operations are: - `set_bit:k`: set the `k`-th bit (0-indexed from the least significant bit) to 1. - `clear_bit:k`: clear the `k`-th bit to 0. - `toggle_bit:k`: toggle (flip) the `k`-th bit. - `get_bit:k`: return the value of the `k`-th bit, 0 or 1. - `check_parity`: return 1 if the number of set bits is odd, 0 if it is even. - `reverse_bits`: reverse all 8 bits of the value. - `count_set_bits`: return the number of 1 bits. - `swap_nibbles`: swap the upper 4 bits and the lower 4 bits. All operations are performed within the 8-bit range (0 to 255). ### Function Signature ```python def bit_operation(n: int, operation: str) -> int: ``` ### Rules - Bit 0 is the least significant bit and bit 7 is the most significant. - For the four operations that take a position, `k` is written as a decimal digit after the colon, for example `"toggle_bit:6"`. The other four operations have no colon. - `reverse_bits` always treats `n` as exactly 8 bits, leading zeros included, so bit `i` moves to bit `7 - i`. For example, `n = 1` becomes 128. - `set_bit`, `clear_bit`, `toggle_bit`, `reverse_bits` and `swap_nibbles` return a value from 0 to 255. `get_bit` and `check_parity` return 0 or 1. `count_set_bits` returns a value from 0 to 8. ### Constraints - `0 <= n <= 255` - `operation` is one of `set_bit:k`, `clear_bit:k`, `toggle_bit:k`, `get_bit:k`, `check_parity`, `reverse_bits`, `count_set_bits`, `swap_nibbles` - `0 <= k <= 7` (when applicable) ### Examples ```text Input: n = 5, operation = "set_bit:1" Output: 7 ``` 5 is `00000101` in binary. Setting bit 1 gives `00000111`, which is 7. ```text Input: n = 183, operation = "reverse_bits" Output: 237 ``` 183 is `10110111` in binary. Reading those 8 bits backwards gives `11101101`, which is 128 + 64 + 32 + 8 + 4 + 1 = 237. ```text Input: n = 180, operation = "swap_nibbles" Output: 75 ``` 180 is `10110100`, so the upper nibble is `1011` and the lower nibble is `0100`. Swapping them gives `01001011`, which is 64 + 8 + 2 + 1 = 75.

Overview: Given an 8-bit unsigned integer and an operation name, return the result of setting, clearing, toggling or reading one bit, checking parity, counting set bits, reversing all 8 bits or swapping the two nibbles. It tests masks, shifts, XOR and fixed-width bit reversal with leading zeros.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an 8-bit unsigned integer n and a string operation, perform the specified bit manipulation operation on n and return the result as an integer.

The supported operations are:

  • set_bit:k : set the k -th bit (0-indexed from the least significant bit) to 1.
  • clear_bit:k : clear the k -th bit to 0.
  • toggle_bit:k : toggle (flip) the k -th bit.
  • get_bit:k : return the value of the k -th bit, 0 or 1.
  • check_parity : return 1 if the number of set bits is odd, 0 if it is even.
  • reverse_bits : reverse all 8 bits of the value.
  • count_set_bits : return the number of 1 bits.
  • swap_nibbles : swap the upper 4 bits and the lower 4 bits.

All operations are performed within the 8-bit range (0 to 255).

Function Signature

def bit_operation(n: int, operation: str) -> int:

Rules

  • Bit 0 is the least significant bit and bit 7 is the most significant.
  • For the four operations that take a position, k is written as a decimal digit after the colon, for example "toggle_bit:6" . The other four operations have no colon.
  • reverse_bits always treats n as exactly 8 bits, leading zeros included, so bit i moves to bit 7 - i . For example, n = 1 becomes 128.
  • set_bit , clear_bit , toggle_bit , reverse_bits and swap_nibbles return a value from 0 to 255. get_bit and check_parity return 0 or 1. count_set_bits returns a value from 0 to 8.

Constraints

  • 0 <= n <= 255
  • operation is one of set_bit:k , clear_bit:k , toggle_bit:k , get_bit:k , check_parity , reverse_bits , count_set_bits , swap_nibbles
  • 0 <= k <= 7 (when applicable)

Examples

Input: n = 5, operation = "set_bit:1"
Output: 7

5 is 00000101 in binary. Setting bit 1 gives 00000111, which is 7.

Input: n = 183, operation = "reverse_bits"
Output: 237

183 is 10110111 in binary. Reading those 8 bits backwards gives 11101101, which is 128 + 64 + 32 + 8 + 4 + 1 = 237.

Input: n = 180, operation = "swap_nibbles"
Output: 75

180 is 10110100, so the upper nibble is 1011 and the lower nibble is 0100. Swapping them gives 01001011, which is 64 + 8 + 2 + 1 = 75.