1297. Copy Sign Bit from One Number to Another

EasyBit ManipulationBit ManipulationSign BitTrick

Given two integers src and val, return a number whose magnitude equals |val| but whose sign matches src (negative if src is negative, otherwise non-negative). Zero is treated as non-negative.

Input: A JSON object {"src": <integer>, "val": <integer>}.

Output: Return |val| with the sign of src.

Examples

Example 1
Input: {"src":-3,"val":5}
Output: -5
Explanation: Magnitude 5 with a negative sign -> -5.
Example 2
Input: {"src":4,"val":-7}
Output: 7
Explanation: Magnitude 7 with a non-negative sign -> 7.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →