dividing of binary numbers
조회 수: 14 (최근 30일)
이전 댓글 표시
See i have a binary code of unknown length , i want to divide it into blocks of length four and assign each block to a variable .
댓글 수: 4
Walter Roberson
2023년 5월 23일
What do you want to have happen if the character vector is not an exact multiple of 4 long?
채택된 답변
Abhishek
2023년 5월 22일
Hi Ganesh,
It is my understanding that you are unable to divide a binary data of unknown length into multiple blocks of length four.
To resolve this issue, you may perform bit operations on the binary data to extract last four bits, then store the result in an array and assign each block to a variable.
For your reference, the below example demonstrates a simple approach to divide a binary data of unknown length to multiple blocks:
% register is the variable that stores binary number.
register = 0b1001011010001010101011010101010110000010101111110101010101;
% An Array to store the binary number of length of four.
blockOfLengthFour = [];
% Simple bit operation to extract last four bits and append them to an
% array
while register
% Get the last four bits
nextFourBits = bitand(register, 15);
% Append the result to final result array
blockOfLengthFour = [nextFourBits, blockOfLengthFour];
% remove last four bits of original binary number
register = bitshift(register, -4);
end
blockOfLengthFour
댓글 수: 6
Abhishek
2023년 5월 22일
The last line of provided code gives 15 different values and each of them is greater than single bit.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Sources에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!