Xor operation on two numbers

조회 수: 24 (최근 30일)
PRAVEEN GUPTA
PRAVEEN GUPTA 2020년 3월 11일
댓글: Pravindra Kumar 2022년 8월 4일
i have two numbers 23 and 47, i have to convert them into binary and then perform the Xor operation how to do that
  댓글 수: 4
Guillaume
Guillaume 2020년 3월 11일
If it were homework, surely the OP wouldn't be cheating by asking us to do it for him/her...
John D'Errico
John D'Errico 2020년 3월 11일
lol

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

채택된 답변

Bhaskar R
Bhaskar R 2020년 3월 11일
편집: Bhaskar R 2020년 3월 11일
Since decimal to binary conversion may not produce same length of inputs to xor, we need to append 0's before the binary value
in1 = 27;
in2 = 47;
x = dec2bin(in1);
y = dec2bin(in2);
if length(x)~=length(y)
max_len = max(length(x), length(y));
x = [repmat('0', 1, max_len-length(x)), x];
y = [repmat('0', 1, max_len-length(y)), y];
end
result = xor(x-'0', y-'0');

추가 답변 (1개)

John D'Errico
John D'Errico 2020년 3월 11일
편집: John D'Errico 2020년 3월 11일
Use the 2 argument form of dec2bin, comverting to binary. This allows you to insure the two binary equivalents are stored with the same number of bits. So one of the binary forms may now have leading zero bits. Otherwise, dec2bin may leave one result too short for the xor. By computing the number of bits necessary in advance for each number, then we can use that 2 argument form for dec2bin.
Next, you need to make the result a logical vector, not the character-form that dec2bin returns. While it looks like a binary number, it is not something that xor can use directly.
Finally, a call to xor does the final piece.
It would look like this:
x = 23;
y = 47;
nbits = floor(log2(max(x,y)))+1;
xb = dec2bin(x,nbits) == '1';
yb = dec2bin(y,nbits) == '1';
result = xor(xb,yb);
Did it work? Yes.
xb
xb =
1×6 logical array
0 1 0 1 1 1
yb
yb =
1×6 logical array
1 0 1 1 1 1
result
result =
1×6 logical array
1 1 1 0 0 0
As you can see, xb now has a leading zero bit, as is needed to make the xor happy. As well, xb and yb are now numeric (logical) vectors, the other thing that xor will need.
If you need to convert result back into a decimal integer form, then use bin2dec. Don't forget to convert the binary form from xor back into characters first, else bin2dec will fail. The idea is to add '0' to the binary form, this gives the ascii numeric form for '0' and '1'. Then the function char takes it to characters, and bin2dec gives you an integer.
bin2dec(char(result + '0'))
ans =
56
Could I have done this more compactly? Well, yes. Two lines would have been sufficient, because I never really needed to compute the number of bits in advance. The trick here is to use dec2bin only once, in a "vectorized" call.
B = dec2bin([x,y]) == '1'
B =
2×6 logical array
0 1 0 1 1 1
1 0 1 1 1 1
result = xor(B(1,:),B(2,:))
result =
1×6 logical array
1 1 1 0 0 0
You can see the vectorized form of dec2bin is smart enough to automatically left pad the smaller number as is needed. That allows us to not need to precompute the number of bits needed. And if I really wanted the result returned in an integer form, I could have done that too.
result = bin2dec(char(xor(B(1,:),B(2,:)) + '0'))
result =
56

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by