Bit xor of two binary strings and conversion into decimal

Hi, I have two large binary strings (e.g 256 bits each), then how to perform bit Xor operation and then convert the answer into decimal number.
str1='11010011110111110000110110111111';
str2='00011111001101011010001111100001';
str3=bitxor(str1,str2);
%%getting error in str3

3 件のコメント

James Tursa
James Tursa 2020 年 6 月 19 日
編集済み: James Tursa 2020 年 6 月 19 日
To be clear, you want a decimal number that can't be represented in any native integer type? A 256-bit integer? Or did you just mean you want the result as a vector of 0's and 1's but not character?
lilly lord
lilly lord 2020 年 6 月 19 日
I want to divide the string into 8 bit chunks and need decimal value between 0-255.
James Tursa
James Tursa 2020 年 6 月 19 日
See my edited answer.

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2020 年 6 月 19 日
編集済み: James Tursa 2020 年 6 月 19 日

0 投票

The xor part as a logical vector result
result = str1 ~= str2;
Or if you wanted it as char:
str3 = char((str1 ~= str2) + '0');
For 8-bit chunks turned into decimal numbers:
result = 2.^(7:-1:0) * reshape(str1 ~= str2,8,[]);

7 件のコメント

lilly lord
lilly lord 2020 年 6 月 19 日
Thanks this part works well. I am using the function below to convert the binary string into dec but error
function dec1 = b2dec(b1,mn)%mn is the dim of matrix i.e 1*8*8 for 8*8 matrix
b2 = reshape(b1,8,mn)';
dec1 = [];
for i=1:mn
x = b2(i,:);
y = bin2dec(x);
dec1 = [dec1 y];
end
lilly lord
lilly lord 2020 年 6 月 19 日
Thank u sir
James Tursa
James Tursa 2020 年 6 月 19 日
This assumes the strings str1 and str2 are rows as your example shows.
lilly lord
lilly lord 2020 年 6 月 19 日
result = 2.^(7:-1:0) * reshape(str1 ~= str2,8,[]);
when I replace str1~=str2 directly with str3 ,I am getting big numbers . Can i use only the output string and get the numbers with in the range of 0-255
lilly lord
lilly lord 2020 年 6 月 19 日
Is there a way in which i can use the output string only.
James Tursa
James Tursa 2020 年 6 月 20 日
So, can you tell me what exactly you have as inputs (size and class) and what exactly you want as an output (size and class)?
lilly lord
lilly lord 2020 年 6 月 21 日
I was workig on random bits generating and try different methods. Your both answer solved my problem . Thanks once again
%%%%Test random bit generation
I=imread('airplane.png');
I2=imresize(I,[128 128]);figure;imshow(I2)
R=I2(:,:,1);G=I2(:,:,2);
R_r=reshape(R,1,[]);
G_row=reshape(G,1,[]);
G_bits=dec2bin(G_row,8);
R_bits=dec2bin(R_r,8);
Red_bits=R_bits(:)';
Gr_bits=G_bits(:)';
%Extract last four bits
R_newbit=R_bits(:,5:end);
R_finabit=R_newbit(:)';
G_newbit=G_bits(:,5:end);
G_finalbit=G_newbit(:)';
s1=length(R_finabit);%check the size of both strings
s2=length(G_finalbit);
%bit Xor g final bit and r final bit and give the answer in dec number
result = 2.^(7:-1:0) * reshape(R_finabit ~= G_finalbit,8,[]);

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2020 年 6 月 19 日

1 投票

Use java BigInteger
import java.math.*;
a=BigInteger(str1,2);
b=BigInteger(str2,2);
c=a.xor(b);%c will be the decimal BigInteger

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2020 年 6 月 19 日

コメント済み:

2020 年 6 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by