The Gray Code Pulse
HardAcc. 81.3%
+40 XP 20
Safe Transitions
Gray code is a binary numeral system where two successive values differ in only one bit. Formula: $G(n) = n oplus (n >> 1)$.
The Assignment
Your function receives a parameter num.
- Manually calculate the Gray code equivalent using loops and bit-like math (or standard operators if allowed, but focus on the conversion logic).
- Binary conversion loop:
- Calculate
num ^ floor(num / 2).
- Calculate
- Print the resulting integer.
01EXAMPLE 1
Input
num = 3Output
2Explanation: 3 is 11, Gray is 10 (2).
Constraints
- Handle small and large integers.
- Manual XOR simulation via math if bitwise not available.
MathLoopsBinary
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
num3
Expected Output
2
Click RUN to test your solution