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.

  1. Manually calculate the Gray code equivalent using loops and bit-like math (or standard operators if allowed, but focus on the conversion logic).
  2. Binary conversion loop:
    • Calculate num ^ floor(num / 2).
  3. Print the resulting integer.

01EXAMPLE 1

Inputnum = 3
Output2

Explanation: 3 is 11, Gray is 10 (2).

Constraints

  • Handle small and large integers.
  • Manual XOR simulation via math if bitwise not available.
MathLoopsBinary
JavaScript
Loading...
3 Hidden

Input Arguments

num3

Expected Output

2

Click RUN to test your solution