Gray Code - Algorithm in Matlab Bin - Dec Conversions
Gray Code - Algorithm in Matlab Bin - Dec Conversions
Home
Gray Code in Matlab from/to
Welcome binary and decimal
Matrixmania Blog The Gray code (also known as
reflected binary code), is a
Sitemap / Search binary numerical system where
two consecutive values differ in
Matlab Books only one bit.
Forums and Help The Gray code code was
originally designed to prevent
Contact undesired transient states or
outputs from electro-
Basics
mechanical switches. Today,
Quick Matlab Guide this code is used to facilitate
error correction in digital
Matlab Tutorial communications and digital-to-
analog converters.
Matlab Examples ANNUAL PRIME MEMBERSHIP 499/ YEAR T&C Apply
Matlab Flow In this article, were going to develop a simple Matlab algorithm to make
conversions between (from/to) binary and gray codes.
Boolean Logic
Matlab GUI
Applications
Calculus
Linear Algebra
Matlab Cookbook I
Matlab Cookbook II
Electrical Calc
Probab and Stats The most significant bit (MSB) in Gray is taken directly from the MSB in binary.
The rest of the Gray bits comes from a xor operation between the precedent
Finance Apps binary bit(b(i-1)) and the current binary bit (b(i)). In the case shown in the
figure above:
Other
Online Calculators g(1) = b(1)
g(2) = b(1) xor b(2)
Relevant Links
g(3) = b(2) xor b(3)
g(4) = b(3) xor b(4)
Notes on Comp
g(5) = b(4) xor b(5)
Fun!
Scilab The xor operation produces a 1 if the bits are different, and produces a 0 if
the bits are equal. So, a binary 11101 becomes a 10011 in Gray.
Your own Website?
Lets propose a code in Matlab to do it.
Terms/Policies
function g = bin2gray(b)
g(1) = b(1);
for i = 2 : length(b);
x = xor(str2num(b(i-1)), str2num(b(i)));
g(i) = num2str(x);
end
http://www.matrixlab-examples.com/gray-code.html 1/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions
g = bin2gray('11101')
g = bin2gray('10010')
g = bin2gray('11110')
g = bin2gray('10000')
g = 10011
g = 11011
g = 10001
g = 11000
for d = 0 : 15
b = dec2bin(d);
g = bin2gray(b);
disp({d b g})
end
Now lets understand the algorithm to go from Gray to binary. See the
conversion from 10011 Gray to its binary equivalent.
The most significant bit (MSB) in binary is taken directly from the MSB in Gray.
The rest of the binary bits comes from a xor operation between the precedent
binary bit (b(i-1)) and the current Gray bit (g(i)). In the case shown in the
figure above:
b(1) = g(1)
b(2) = b(1) xor g(2)
b(3) = b(2) xor g(3)
b(4) = b(3) xor g(4)
b(5) = b(4) xor g(5)
http://www.matrixlab-examples.com/gray-code.html 2/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions
function b = gray2bin(g)
b(1) = g(1);
for i = 2 : length(g);
x = xor(str2num(b(i-1)), str2num(g(i)));
b(i) = num2str(x);
end
b = gray2bin('10011')
b = gray2bin('11011')
b = gray2bin('10001')
b = gray2bin('11000')
b = 11101
b = 10010
b = 11110
b = 10000
for d = 0 : 15
g = bin2gray(dec2bin(d));
b = gray2bin(g);
disp({d g b})
end
http://www.matrixlab-examples.com/gray-code.html 3/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions
Top
Binary-to-dec
Decimal-to-bin
Bin-to-hexadecimal
Hex-to-bin
http://www.matrixlab-examples.com/gray-code.html 4/4