How can i XOR these two numbers?

3 次查看(过去 30 天)
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 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by