Design a code converter that converts a 4-bit number from Gray code to binary co
ID: 3638270 • Letter: D
Question
Design a code converter that converts a 4-bit number from Gray code to binary code.Please help!
Explanation / Answer
Hi, my friend this is the program u want if u need more help message me Note: the porgram is in java and c++ Java package utilities.numeric; /** * Contains methods to convert between Gray code and natural binary values. * @author Stephen Luce */ public class GrayCode { /** * Converts a natural binary 32 bit value to Gray code. * @param natural The natural binary value to convert. * @return The Gray code equivalent value. */ public static int encodeGray(int natural) { return natural ^ natural >>> 1; } /** * Converts a Gray code 32 bit value to natural binary. * @param gray The Gray code value to convert. * @return The natural binary equivalent value. */ public static int decodeGray(int gray) { int natural = 0; while (gray != 0) { natural ^= gray; gray >>>= 1; } return natural; } /** * Converts a natural binary 64 bit value to Gray code. * @param natural The natural binary value to convert. * @return The Gray code equivalent value. */ public static long encodeGray(long natural) { return natural ^ natural >>> 1; } /** * Converts a Gray code 64 bit value to natural binary. * @param gray The Gray code value to convert. * @return The natural binary equivalent value. */ public static long decodeGray(long gray) { long natural = 0; while (gray != 0) { natural ^= gray; gray >>>= 1; } return natural; } } c++ #include #include void main() {int a[10],i=0,c=0,n,b[10]; printf(" enter the binary code"); scanf("%d",&n); while(n!=0) {a[i]=n%10; n/=10; i++; c++; } for(i=c-1;i>=0;i--) { b[i]=a[i]; } for(i=c-1;i>=0;i--) { if(b[i]==1) { if(a[i-1]==1) a[i-1]=0; else a[i-1]=1; } } printf(" the gray code is"); for(i=c-1;i>=0;i--) printf("%d",a[i]); getch(); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.