how to swap two bits in matlab.? which cammond is used for this..?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sultan Mehmood
am 11 Jun. 2019
Kommentiert: Sultan Mehmood
am 12 Jun. 2019
Input: n = 28, p1 = 0, p2 = 3
Output: 21
28 in binary is 11100. If we swap 0'th and 3rd digits,
we get 10101 which is 21 in decimal.
0 Kommentare
Akzeptierte Antwort
KALYAN ACHARJYA
am 11 Jun. 2019
>> a=de2bi(28)
a =
0 0 1 1 1
Here swap first and Fifth Elements (Change as per your requirements)
>> a([1 5])=a([5 1])
a =
1 0 1 1 0
>> a=bi2de(a)
a =
13
>>
2 Kommentare
Weitere Antworten (1)
Chirag Nighut
am 11 Jun. 2019
Remember that Matlab has 1 based indexing and therfore when you wish to swap the 0th and 3rd bit in above number, it is actually bit number 1 and 4.
Following code should work:
p1 = 1;
p2 = 4;
A = 28;
d = de2bi(A);
d([p1 p2]) = d([p2 p1]);
B = bi2de(d)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!