how to swap two bits in matlab.? which cammond is used for this..?

조회 수: 14 (최근 30일)
Sultan Mehmood
Sultan Mehmood 2019년 6월 11일
댓글: Sultan Mehmood 2019년 6월 12일
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.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 11일
>> 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
Sultan Mehmood
Sultan Mehmood 2019년 6월 12일
how to apply 2 by 2 matrix.?
Sultan Mehmood
Sultan Mehmood 2019년 6월 12일
A=[21 28 54;25 27 54;29 21 76];
d=de2bi(A)
k=1:9;
L=[2,8,3,5,5,3,2,3,7];
P=[5,6,7,7,1,8,2,2,2];
d([L(k) P(k)]) = d([P(k) L(k)]);
B = bi2de(d);

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

추가 답변 (1개)

Chirag Nighut
Chirag Nighut 2019년 6월 11일
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)
  댓글 수: 1
Sultan Mehmood
Sultan Mehmood 2019년 6월 12일
A=[21 28 54;25 27 54;29 21 76];
L=[2,8,3,5,5,3,2,3,7];
P=[5,6,7,7,1,8,2,2,2];
how we apply on this.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by