← DiffPush

Bit Manipulation

Baseline Bit Manipulation · Learn Bit Manipulation O(1) · O(1)

Wiring One Relay on the Lamp Panel

A control room keeps a bank of numbered relays encoded as a 32-bit integer, and the technician must inspect, energize, and cut a single relay line in one shift. Relays are numbered from the nearest end starting at one, not zero. Report the relay's current state, the integer after energizing it, and the integer after cutting it.

Input: An integer num (the relay bank) and an integer i (1-based relay position).

Output: A triple [state, num after set, num after clear].

Constraints

Examples

Example 1

Input: {"num":70,"i":3}
Output: [1,70,66]
70 is 1000110 in binary; the 3rd relay is already on, so setting changes nothing and cutting yields 66.

Example 2

Input: {"num":10,"i":2}
Output: [1,10,8]
10 is 1010; the 2nd relay is on, and cutting it drops the bank to 8.

Solve this in your browser →

Also on LeetCode ↗