-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_gray-to-binary-equivalent.java
56 lines (45 loc) · 1.5 KB
/
11_gray-to-binary-equivalent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 04-2024(April)/11_gray-to-binary-equivalent.java
/**
* Date : 11-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Gray to Binary equivalent
* Difficulty: 🟡Medium
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/gray-to-binary-equivalent-1587115620/1
*/
// User function Template for Java
class Solution {
// function to convert a given Gray equivalent n to Binary equivalent.
// Gray code is a binary numeral system where two successive values differ in only one bit.
// This means that each bit in the Gray code is formed by taking the XOR of the corresponding bit
// in the binary number and the bit to its right in the binary number.
public static int grayToBinary(int n) {
int ans = n;
// Right Shift(>>) will remove the MSB to the next bit ; As ans = n, so XOR with corresponding
// bit Right shift n by 1 and XOR it to ans until n becomes zero
while (n != 0) {
n = n >> 1;
ans = ans ^ n;
}
// return binary equivalent of gray code
return ans;
}
}
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); // testcases
while (t-- > 0) {
int n = sc.nextInt(); // initializing n
Solution obj = new Solution();
// calling grayToBinary() function
System.out.println(obj.grayToBinary(n));
}
}
}
// } Driver Code Ends