1298. Merge Bits from Two Values at a Mask

EasyBit ManipulationBit ManipulationBit MaskingMerge

Given integers a, b, and mask, produce a value that takes each bit from b where the corresponding mask bit is 1, and from a where the mask bit is 0.

Input: A JSON object {"a": <integer>, "b": <integer>, "mask": <integer>}.

Output: Return the merged value (a where mask=0, b where mask=1).

Examples

Example 1
Input: {"a":12,"b":10,"mask":6}
Output: 10
Explanation: Bits of b are taken at mask positions, giving 1010 = 10.
Example 2
Input: {"a":15,"b":0,"mask":10}
Output: 5
Explanation: Clears a's bits where mask=1 -> 0101 = 5.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →