binary bits xor operation
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!