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

18 views (last 30 days)
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')
  1 Comment
Guillaume
Guillaume on 3 Jul 2019
Edited: Guillaume on 3 Jul 2019
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).

Sign in to comment.

Answers (2)

Basil C.
Basil C. on 3 Jul 2019
I suppose the error has to do with the command 'fliplr'
Could you rather try the function 'flipud'
X = flipud(b);

Abhishek Kumar
Abhishek Kumar on 3 Jul 2019
Edited: Abhishek Kumar on 3 Jul 2019
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.
for more info try : >>help 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

Community Treasure Hunt

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

Start Hunting!