can anyone suggest me how to convert binary value to gray code value??

49 views (last 30 days)
i would also like to know if you can provide me with the reverse conversion code of gray to binary value...thank you in advance.. :)

Answers (4)

Guillaume
Guillaume on 29 Apr 2016
The wikipedia article gives a very simple algorithm. I'm not sure what the difficulty is in converting it to matlab:
function num = GrayToBinary(num)
mask = bitshift(num, -1);
while mask > 0
num = bitxor(num, mask);
mask = bitshift(mask, -1);
end
end

Image Analyst
Image Analyst on 29 Apr 2016
In the Communications Systems Toolbox:
bin2gray
Convert positive integers into corresponding Gray-encoded integers
Syntax y = bin2gray(x,modulation,M) [y,map] = bin2gray(x,modulation,M)
Description y = bin2gray(x,modulation,M) generates a Gray-encoded vector or matrix output y with the same dimensions as its input parameter x. x can be a scalar, vector, or matrix. modulation is the modulation type and must be a string equal to 'qam', 'pam', 'fsk', 'dpsk', or 'psk'. M is the modulation order that can be an integer power of 2.
  2 Comments
Rezaur Rahman
Rezaur Rahman on 29 Apr 2016
please do give the code...it will be a great help for me...and i dnt understand why the modulation is needed for the conversion...anyways, thanx... :)
Image Analyst
Image Analyst on 29 Apr 2016
I don't have that toolbox. If you do, then in the help there is nearly always example code. You can ask for a trial version for a month if you want. It's free for a month. Good luck.

Sign in to comment.


Idin Motedayen-Aval
Idin Motedayen-Aval on 3 Sep 2025 at 17:46
bin2gray is no longer a MATLAB function, however, most of the digital modulation functions in Communication Toolbox (e.g. PAM, PSK, etc.) support gray coding directly. This example is a good reference:
If you really just want to convert binary mapping to gray (i.e. generate the table in the figure), you could either just write the lookup table manually, or do something like this:
M = 16; % Modulation order or 2^numBits
x = pskmod(0:M-1,M,0,"gray").';
y = pskdemod(x,16,0,"bin") % Will output the gray ordered integers
b = dec2bin(y) % Will output binary values
(I can't immediately think of why one might want to do this; other than as a teaching tool.)
Also note that binary-to-gray mapping is not unique. So the ordering you get from the code above may not match someone else's gray coding.

David Goodmanson
David Goodmanson on 3 Sep 2025 at 21:03
Edited: David Goodmanson 8 minutes ago
Here I used 'grey' instead of gray since Matlab has a function called gray.
n = 4
G = grey(n);
IG = igrey(n);
nbin = dec2bin(0:2^n-1);
[nbin blankss(2^n,2) G blankss(2^n,2) IG]
ans = 16×16 char array
'0000 0000 0000'
'0001 0001 0001'
'0010 0011 0011'
'0011 0010 0010'
'0100 0110 0111'
'0101 0111 0110'
'0110 0101 0100'
'0111 0100 0101'
'1000 1100 1111'
'1001 1101 1110'
'1010 1111 1100'
'1011 1110 1101'
'1100 1010 1000'
'1101 1011 1001'
'1110 1001 1011'
'1111 1000 1010'
fist column is binary 0:2^n-1, second column is grey code, third column is inverse grey code. example:
a = 12
abin = dec2bin(a)
abin = '1100'
bbin = G(a+1,:) % grey code; table is zero-based so add 1 to index
bbin = '1010'
b = bin2dec(bbin)
b = 10
cbin = IG(b+1,:) % inverse grey, add 1 to index
cbin = '1100' % cbin = abin
c = bin2dec(cbin)
c = 12
%-----------------
function c = grey(n)
% n-digit gray code, 2^n row string matrix
zero = '0'; one = '1';
c = [zero one]';
for k=2:n
a = ones(2^(k-1),1);
c = [zero(a), c; one(a), flipud(c)];
end
end
function c = igrey(n)
% n-digit inverse gray code, 2^n row string matrix
zero = '0'; one = '1';
c = [zero one]';
for k=2:n
a = ones(2^(k-1),1);
d = flipud(c);
d(:,2:2:end) = complement(d(:,2:2:end));
c = [zero(a), c; one(a), d];
end
end
% a different way that bypasses explict construction of igrey
function c = igrey(n)
% n-digit inverse gray code, 2^n row string matrix
% by simple inverse permutation of the grey code indices
a = bin2dec(grey(n));
[~, b] = sort(a);
c = dec2bin(b-1);
end
function xbar = complement(x)
xbar = char(97-abs(x)); % '0' <-> '1'
end
function b = blankss(m,n)
% matrix of blanks, using Tony's trick!
% b = blankss(m,n)
space = ' ';
b = space(ones(m,n));
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!