Hello,
I need to convert n-bit decimal into 2^n bit binary number. I do not have much idea. Can anybody help me please?

댓글 수: 4

Jan
Jan 2019년 1월 22일
What exactly is a "n bit decimal"? Integer or floating point values? What about dec2bin?
Sky Scrapper
Sky Scrapper 2019년 1월 22일
편집: Sky Scrapper 2019년 1월 22일
It is integer. I have tried:
n= 8;
for i = 0:2^n-1
x = dec2bin(i,8);
end
It's showing, x= 11111111
But I need the values of x=0.......2^8 in binary
Jan
Jan 2019년 1월 22일
2^8 or 2^8-1 ?
Stephen23
Stephen23 2019년 1월 22일
편집: Stephen23 2019년 1월 22일
Get rid of the loop:
>> V = 0:pow2(8)-1;
>> dec2bin(V)
ans =
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
... lots of rows here
11111010
11111011
11111100
11111101
11111110
11111111

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2019년 1월 22일
편집: Jan 2021년 11월 1일

0 개 추천

This code shows '11111111' only, because you overwrite the output in each iteration:
n= 8;
for i = 0:2^n-1
x = dec2bin(i,8);
end
Therefore x contains the last value only: dec2bin(2^n-1, 8).
Better:
x = dec2bin(0:2^n-1, 8);
Or if you really want a loop:
n = 8;
x = repmat(' ', 2^n-1, 8); % Pre-allocate
for i = 0:2^n-1
x(i+1, :) = dec2bin(i,8);
end
x
[EDITED] If you want the numbers 0 and 1 instead of a char matrix with '0' and '1', either subtract '0':
x = dec2bin(0:2^n-1, 8) - '0';
But to avoid the conversion to a char and back again, you can write an easy function also:
function B = Dec2BinNumeric(D, N)
B = rem(floor(D(:) ./ bitshift(1, N-1:-1:0)), 2);
end
% [EDITED] pow2(n) reülaced by faster bitshift(1, n)
PS. You see, the underlying maths is not complicated.

댓글 수: 9

Sky Scrapper
Sky Scrapper 2019년 1월 22일
편집: Sky Scrapper 2019년 1월 22일
yes, that's working. Thank you so much.
Sky Scrapper
Sky Scrapper 2019년 1월 22일
Problem is that it's showing string value say, ''11111111'' but i will have to get double array something like ''1 1 1 1 1 1 1 1''. i think it's not possible using dec2bin. I am using Matlab2016a. so it's not possible to use ''decimalToBinaryVector''. colud you please help me know in this regard?
Jan
Jan 2019년 1월 22일
편집: Jan 2019년 1월 22일
To convert from '1010' to [1 0 1 0], see [EDITED] in my answer.
Sky Scrapper
Sky Scrapper 2019년 1월 23일
If i run your function code it's showing error,''Not enough input arguments.''
Walter Roberson
Walter Roberson 2019년 1월 23일
What arguments did you pass to Dec2BinNumeric ?
Call it e.g. like:
B = Dec2BinNumeric(17, 8)
Sky Scrapper
Sky Scrapper 2019년 1월 23일
i will have to convert for ''n'' having higher values as like n=1000000.
Anyway, ''x = dec2bin(0:2^n-1, 8) - '0';'' this is working properly. thanks!
Walter Roberson
Walter Roberson 2019년 1월 23일
A one-million bit binary number cannot be converted to a double precision value.
If
dec2bin(0:2^n-1, 8) - '0'
is working, calling
Dec2BinNumeric(0:2^n-1, 8)
is not a serious difference.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

PRAVEEN GUPTA
PRAVEEN GUPTA 2019년 7월 8일

0 개 추천

i have string of number [240 25 32 32]
i want to convert them in binary
how can i do this???

댓글 수: 2

Jan
Jan 2019년 7월 8일
Do no attach a new question as an asnwer of another one.
Did you read this thread? dec2bin has been suggested already, as well as a hand made solution Dec2BinNumeric. Simply use them.
AB WAHEED LONE
AB WAHEED LONE 2021년 3월 6일
편집: AB WAHEED LONE 2021년 3월 6일
I know it is late but somwhow it may help
bin_array=dec2bin(array,8)-'0';

댓글을 달려면 로그인하십시오.

vandana Ananthagiri
vandana Ananthagiri 2020년 2월 5일

0 개 추천

function A = binary_numbers(n)
A = double(dec2bin(0:((2^n)-1),n))-48;
end

댓글 수: 2

Walter Roberson
Walter Roberson 2020년 2월 5일
Why 48?
I know the answer, but other people reading your code might not, so I would recommend either a comment or a different representation.
vincent voogt
vincent voogt 2021년 11월 1일
Maybe a late reply, but dec2bin return as string of ASCII characters, where 0-9 are mapped on character number 48-57.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2019년 1월 22일

편집:

Jan
2021년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by