# Decimal to binary, bit reversal of binary and binary to decimal

68 ビュー (過去 30 日間)
Riddhi Padariya 2019 年 7 月 3 日

Hi,
I would like to get the feedback on the following code. I am getting the incorrect number when I convert the binary numbers back to decimal at the end.
First of all, I have converted 1 through 1024 decimal numbers to binary and stored that to b. After that I performed the bit reversal using the fliplr command. I am getting the correct information until this point. But, when I tried to convert those bit reversed numbers to Decimal, I am not getting the correct numbers back . Any feedback on this? Help is much appreciated.
Code:
clear all
d = (1:1024)';
b = de2bi(d,'left-msb');
X = fliplr(b);
Y= bi2de(X, 'left-msb')

#### 2 件のコメント

Guillaume 2019 年 7 月 3 日
I don't have the relevant toolbox to test, but it seems to me the above could be simplified to:
d = (1:1024)';
b = de2bi(d, 'left-msb');
Y = bi2de(b, 'right-msb');
Now, with your statement "I am not getting the correct numbers back". Are you not getting a sequence starting with:
1024
512
1536
256
1280
768
1792
If that's what you're getting, then it is correct (and it's your expectations that are wrong).
Riddhi Padariya 2019 年 7 月 3 日
The code that you are using won't work, because MATLAB adds "0" MSB prior to 10 bits of number starting from 0 to 1023, When MATLAB converts the decimal to binary. The 1024 won't require 0 as MSB just becuase it already has 11 numbers (10000000000). This is something MATLAB does it by default and it presents the problem in this situation. If you want to observe the correct answer you must use d = (0:1023). Run the code again with this change and your code would give you the correct results. If you want to understand the problem in detail then observe the Vector values of b, X and Y before and after conversion. You will get a clear idea.
Code:
clear all;
d = (0:1023)'
b = de2bi(d,'left-msb')
X = fliplr(b)
Y= bi2de(X, 'left-msb')
Enjoy learning!

サインインしてコメントする。

### 回答 (2 件)

Be Matlabi 2019 年 7 月 3 日
I suppose the error has to do with the command 'fliplr'
Could you rather try the function 'flipud'
X = flipud(b);

#### 1 件のコメント

Riddhi Padariya 2019 年 7 月 3 日
Nope. That does not solve the issue. Thank you for your feedback. Please review the answer I posted.

サインインしてコメントする。

Abhishek Kumar 2019 年 7 月 3 日

Hi Riddhi
What is the output you are getting and what is the output that you think you should get?
I tried running the same program, I was getting the right output.
X stores the flipped bits, so when you convert it to decimal, you get the output of the flipped bits.
If you want the order in reversed fashion, try flipud.
Below, is my code's output.
>> d
d =
1
2
3
4
5
6
7
8
9
10
>> b
b =
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
>> X
X =
1 0 0 0
0 1 0 0
1 1 0 0
0 0 1 0
1 0 1 0
0 1 1 0
1 1 1 0
0 0 0 1
1 0 0 1
0 1 0 1
>> Y
Y =
8
4
12
2
10
6
14
1
9
5

#### 2 件のコメント

Riddhi Padariya 2019 年 7 月 3 日
I got the same answer as well. But,what we are getting in Vector Y is wrong. Please understand what I mean by that from the example.
i.e.
Binary of 7 is 111 and bit reversal of 111 is 111 and whenever we convert it back to decimal it should be 7. But instead of 7 we are getting 14. It is because MATLAB is convering the binary of 7 as 0111 and bit reversal of 0111 is 1110 and if we would convert that back to decimal we will get 14. That's the wrong answer. I should get 7 not 14. So, I meant that is there any way that we can use as many bit is required when we convert the decimal to binary so we don't have to deal with this issue when we follow the reverse procedure. Please provide me your feedback.
Riddhi Padariya 2019 年 7 月 3 日
This problem is also persistent with other numbers as well, not for number 7 only. Thank you for your time.
Just to support the above written comment. I am getting the correct results, if I am making the range of d upto 7. Because any number higher than 7 would require 4 bits for binary represenation. The conversion that I am performing won't have to deal with that "0" LSB. So, I won't have that problem. Also, I have provided another example doing the Dec2Bin, bit reversal of those 16 numbers and bin2dec for the range of d from 0 to 15. Please see following results
clear all;
d = (0:7)'
b = de2bi(d,'left-msb')
X = fliplr(b)
Y= bi2de(X, 'left-msb')
Simulation Result:
d =
0
1
2
3
4
5
6
7
b =
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
X =
0 0 0
1 0 0
0 1 0
1 1 0
0 0 1
1 0 1
0 1 1
1 1 1
Y =
0
4
2
6
1
5
3
7
Another Example:
clear all;
d = (0:15)'
b = de2bi(d,'left-msb')
X = fliplr(b)
Y= bi2de(X, 'left-msb')
Simulation Result:
d =
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
b =
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
X =
0 0 0 0
1 0 0 0
0 1 0 0
1 1 0 0
0 0 1 0
1 0 1 0
0 1 1 0
1 1 1 0
0 0 0 1
1 0 0 1
0 1 0 1
1 1 0 1
0 0 1 1
1 0 1 1
0 1 1 1
1 1 1 1
Y =
0
8
4
12
2
10
6
14
1
9
5
13
3
11
7
15
This solves the problem. We need to be mindful in order to choose the range of the d. It won't be problem if it has been choosen carefully. Thanks!

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by