Create a Gray Code in C# – Programming, Pseudocode Example, C# Programming Example
C# C# Console

Create a Gray Code in C#

In this C# program, using for loop we are entering the number of elements. The grayEncode() function is used to convert the number into gray code.

A Gray code is an encoding of numbers so that adjacent numbers have a single digit differing by 1.

The term Gray code is often used to refer to a “reflected” code, or more specifically still, the binary reflected Gray code.

In grayDecode() function compute the Binary Left Shift Operator.

The left operands value is moved left by the number of bits specified by the right operand. Multiply the resulted value with 64. Compute the difference between the resulted values by 2.

Using while loop compute the Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Check the resulted value is greater than 0, if the condition is true then execute the statement and compute the Binary Right Shift Operator.

The left operands value 1 is moved right by the number of bits specified by the right operand value of ‘p’ variable, Using Binary AND Operator copy a bit to the result if it exists in both operands.

Convert the resulted value to Binary OR Operator. Copy a bit if it exists in either operand to ‘b’ variable. Print the gray code of the number.

 

Output:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: