binary bits xor operation

2 次查看(过去 30 天)
Rajesh Burada
Rajesh Burada 2020-2-26
回答: BhaTTa 2024-6-14
I have 1 1 0 1 1 0 1 0 8bits binary number now i have to do xor operation to that binary values from the 4th bit to 8th bit and finally i have to get 1 or 0 as output. i used for loop but its taking time, any other solutions for fastest way?
A=imread('C:\Users\asus\Desktop\lena.png') %read image
x=de2bi(A,8);
for i=1:65536
k=3
for j=1:1
k=k+1
z1=xor(x(i,k),x(i,k+1))
z1=xor(z1,x(i,k+2))
z1=xor(z1,x(i,k+3))
z1=xor(z1,x(i,k+4))
end
end

回答(1 个)

BhaTTa
BhaTTa 2024-6-14
For performing an XOR operation on specific bits of binary numbers efficiently, without using a loop for the bit-wise operation, you can take advantage of MATLAB's vectorized operations. Given that you're interested in XORing bits from the 4th to the 8th bit of each 8-bit binary number, you can achieve this with a more direct approach.
Given that x is a matrix where each row represents an 8-bit binary number (as obtained from de2bi function on an image), you can directly perform the XOR operation across the columns of interest. Here's how you can do it efficiently:
% Assuming x is obtained from de2bi(A,8)
% A = imread('C:\Users\asus\Desktop\lena.png'); % Example for reading image
% x = de2bi(A,8); % Convert image to binary representation
% Directly perform XOR operation on the 4th to 8th bits
result = x(:,4); % Initialize result with the 4th column
for k = 5:8 % Loop from the 5th to the 8th column
result = xor(result, x(:,k)); % Perform XOR operation
end
% 'result' now contains the XOR result for each row
This method initializes result with the 4th column of x and then iteratively performs the XOR operation with the next columns (5th to 8th). This way, you avoid the inner loop entirely and only iterate through the columns once.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by