How can i XOR these two numbers?

3 views (last 30 days)
Darsana P M
Darsana P M on 24 Oct 2017
Commented: Darsana P M on 24 Oct 2017
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 Comments
Darsana P M
Darsana P M on 24 Oct 2017
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 on 24 Oct 2017
Edited: Jan on 24 Oct 2017
@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.

Sign in to comment.

Accepted Answer

Jan
Jan on 24 Oct 2017
Edited: Jan on 24 Oct 2017
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 Comments
Jan
Jan on 24 Oct 2017
@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 on 24 Oct 2017
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??

Sign in to comment.

More Answers (1)

KSSV
KSSV on 24 Oct 2017
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 Comment
Darsana P M
Darsana P M on 24 Oct 2017
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)) ;

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!