How can i XOR these two numbers?

조회 수: 4 (최근 30일)
Darsana P M
Darsana P M 2017년 10월 24일
댓글: Darsana P M 2017년 10월 24일
I have 2 sets of values: X =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
U =
01011000
11100010
11111100
11001110
11111010
01111110
00110000
01100001
00110110
01111111
00011101
01010111
10100100
11100111
01000101
01011010
I want to XOR X and U.
I tried value=xor(X-'0',U-'1');
But i got an error. What must I do?
  댓글 수: 3
Darsana P M
Darsana P M 2017년 10월 24일
When the input was
X=[1 1 1 0 0 0 1 0];
U=[0 0 0 0 0 0 0 0];
val=xor(X-'0',U-'1');
Using the above code, gave me the XORed value.
But For this case, I think this logic is wrong. What is the other method?
Jan
Jan 2017년 10월 24일
편집: Jan 2017년 10월 24일
@Darsana P: No, it doesn't. It would work only if the inputs are char vectors:
X = '11100010';
But if X is a numerical vector already, subtracting '0' is meaningless. Subtracting '1' is not useful at all. In other words: "- '0'" is used to convert a char vector containing digits to a numerical vector containing the corresponding numbers.
Copying some code, which you do not understand, is guessing. Guessing is not useful for programming. You can check, what you are doing easily:
X - '0'
U - '1'
The result looks weird, doesn't it? But this works directly:
X = [1 1 1 0 0 0 1 0];
U = [0 0 0 0 0 0 0 0];
val = xor(X, U);
Well, xor'ing with zeros is not that interesting.

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

채택된 답변

Jan
Jan 2017년 10월 24일
편집: Jan 2017년 10월 24일
What are the types of X and U? Are they double or char vectors? Please post the input, such that it can be used by copy&paste.
X = [0; 0; 1] ;
U = [0 1 0 1 1 0 0 0; ...
1 1 1 0 0 0 1 0; ...
1 1 1 1 1 1 0 0];
Result = [U(:, 1:end-1), xor(X, U(:, end))]
Or perhaps:
X = ['0'; '0'; '1'] ;
U = ['01011000'; ...
'11100010'; ...
'11111100'];
Xd = X - '0'; % Convert CHAR '01' to DOUBLE [0, 1]
Ud = X - '0';
Result = [Ud(:, 1:end-1), xor(Xd, Ud(:, end))]
It would be easier to find a solution if you explain, what you want as result.
  댓글 수: 3
Jan
Jan 2017년 10월 24일
@Darsana P M: This is still no valid Matlab code. What does
U =[01011000
11100010]; % Abbreviated
mean? Numbers do not have leading zeros. So perhaps this is a char matrix?
U =['01011000'; ...
'11100010'];
The quotes are really important here. Or perhaps you mean a numerical matrix:
U =[0 1 0 1 1 0 0 0; ...
1 1 1 0 0 0 1 0];
I still assume that your problem is based on a confusion of the types. So please concentrate on this detail.
{'00', '00'} is a very strange input for an AES encryption. Perhaps these are HEX numbers? It is much easier to let AES operate on bytes, UINT8 arrays. Because the bit pattern is completely mixed by this encryption, operating with bit matrices (in which type ever) seems odd here.
Darsana P M
Darsana P M 2017년 10월 24일
The input is hex numbers. That is why i used cell arrays as {'00','00'...} and so on. And for calculation purpose i convert them to hex2dec(). Thus i operate the aes encryption.
Next I convert the aes encrypted value,ie w to binary. Similarly I convert the input x which is now decimal to binary. Thus I get U. So now XOR operation can be done on binary numbers. The code you provided above helped me in solving the problem.
X = ['0'; '0'; '1'] ;
U = ['01011000'; ...
'11100010'; ...
'11111100'];
Xd = X - '0'; % Convert CHAR '01' to DOUBLE [0, 1]
Ud = X - '0';
Result = [Ud(:, 1:end-1), xor(Xd, Ud(:, end))]
I lacked concept problems at first. But now I think I am correct, is it? Now I think the input type is OK??

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

추가 답변 (1개)

KSSV
KSSV 2017년 10월 24일
X = [0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1] ;
U =[01011000
11100010
11111100
11001110
11111010
01111110
00110000
01100001
00110110
01111111
00011101
01010111
10100100
11100111
01000101
01011010] ;
res = xor(num2str(X),num2str(U))
  댓글 수: 1
Darsana P M
Darsana P M 2017년 10월 24일
Still i got an error: Error using xor Inputs must have the same size.
Error in counter1 (line 30) value=xor(num2str(XX),num2str(U)) ;

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by