diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.c b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.c new file mode 100644 index 0000000000..a81777b772 --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.c @@ -0,0 +1,27 @@ +#include + +// Binary to Gray +int binaryToGray(int n) { + return n ^ (n >> 1); +} + +// Gray to Binary +int grayToBinary(int n) { + int binary = n; + while (n > 0) { + n >>= 1; + binary ^= n; + } + return binary; +} + +int main() { + int num = 7; // Binary: 111 + int gray = binaryToGray(num); + int binary = grayToBinary(gray); + + printf("Gray Code: %d\n", gray); // Output: 4 + printf("Binary Code: %d\n", binary); // Output: 7 + + return 0; +} diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.cpp b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.cpp new file mode 100644 index 0000000000..d87f20d5ee --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +// Binary to Gray +int binaryToGray(int n) { + return n ^ (n >> 1); +} + +// Gray to Binary +int grayToBinary(int n) { + int binary = n; + while (n > 0) { + n >>= 1; + binary ^= n; + } + return binary; +} + +int main() { + int num = 7; + cout << "Gray Code: " << binaryToGray(num) << endl; // Output: 4 + cout << "Binary Code: " << grayToBinary(binaryToGray(num)) << endl; // Output: 7 + return 0; +} diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.hs b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.hs new file mode 100644 index 0000000000..642572852d --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.hs @@ -0,0 +1,15 @@ +-- Binary to Gray +binaryToGray :: Int -> Int +binaryToGray n = n `xor` (n `shiftR` 1) + +-- Gray to Binary +grayToBinary :: Int -> Int +grayToBinary 0 = 0 +grayToBinary g = foldl xor 0 (takeWhile (>0) (iterate (`shiftR` 1) g)) + +main :: IO () +main = do + let num = 7 + let gray = binaryToGray num + print ("Gray Code: " ++ show gray) -- Output: 4 + print ("Binary Code: " ++ show (grayToBinary gray)) -- Output: 7 diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.java b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.java new file mode 100644 index 0000000000..c4047da953 --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.java @@ -0,0 +1,23 @@ +public class graycode_conversion { + // Binary to Gray + public static int binaryToGray(int n) { + return n ^ (n >> 1); + } + + // Gray to Binary + public static int grayToBinary(int n) { + int binary = n; + while (n > 0) { + n >>= 1; + binary ^= n; + } + return binary; + } + + public static void main(String[] args) { + int num = 7; + int gray = binaryToGray(num); + System.out.println("Gray Code: " + gray); // Output: 4 + System.out.println("Binary Code: " + grayToBinary(gray)); // Output: 7 + } +} diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.js b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.js new file mode 100644 index 0000000000..11d9e9a322 --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.js @@ -0,0 +1,18 @@ +// Binary to Gray +function binaryToGray(n) { + return n ^ (n >> 1); +} + +// Gray to Binary +function grayToBinary(n) { + let binary = n; + while (n > 0) { + n >>= 1; + binary ^= n; + } + return binary; +} + +let num = 7; +console.log("Gray Code:", binaryToGray(num)); // Output: 4 +console.log("Binary Code:", grayToBinary(binaryToGray(num))); // Output: 7 diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.php b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.php new file mode 100644 index 0000000000..c9a27e674c --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.php @@ -0,0 +1,20 @@ +> 1); +} + +// Gray to Binary +function grayToBinary($n) { + $binary = $n; + while ($n > 0) { + $n >>= 1; + $binary ^= $n; + } + return $binary; +} + +$num = 7; +echo "Gray Code: " . binaryToGray($num) . "\n"; // Output: 4 +echo "Binary Code: " . grayToBinary(binaryToGray($num)) . "\n"; // Output: 7 +?> diff --git a/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.py b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.py new file mode 100644 index 0000000000..bf1308047c --- /dev/null +++ b/code/bit_manipulation/src/binary-graycode-conversion/graycode_conversion.py @@ -0,0 +1,11 @@ +def gray_to_binary(n): + binary = n + while n > 0: + n >>= 1 + binary ^= n + return binary + +# Example +gray_num = 4 # Gray: 100 +binary_num = gray_to_binary(gray_num) +print(bin(binary_num)) # Output: 0b111