Truncate lowest 8 bit of any real valued sequence.

3 views (last 30 days)
Hello.
I am working with a dataset containing integers and floating point numbers. From the dataset, I can extract 4 data sequence x,y,z,w. Suppose I need to first convert the dataset sequences (x,y,z,w) to integer values and then we take 8 least significant bits for all, and then split joint them (in cascade manner) so that I can obtain 32 bits binary sequence at each iteration. How can I do that?
Thankyou in Advance.

Accepted Answer

Arun
Arun on 16 Feb 2024
Hi Saikat,
I understand that from a dataset containing integers and floating points numbers. You wish to extract four elements from the dataset, truncate them to integer values and then split join the least significant bits of those four elements to obtain a 32-bits binary sequence.
Here are the steps that one can follow to do the required task:
  1. Truncate the elements selected using “fix” function.
  2. Find the 8-least significant bits for each element using bitwise AND operation.
  3. Joint the 8-least significant bits to form a 32-bits element.
Here is a sample code for the above-mentioned steps:
% Example vector of integers
elements = [15, 22.5, 5, 8]; % These are the numbers from the sequence
%======step:1=====
%turncate the elements to integers
elements = fix(elements);
%======step-2=======
% Extract the LSB from each number using bitwise AND with 255
lsbs = dec2bin(bitand(elements, 255),8);
% Convert the LSBs to a string to concatenate them
lsbsStr = num2str(lsbs);
%=======step-3======
%join the lsb to for a 32-bit number
% Remove spaces from the string
lsbsStr = join(string(lsbsStr),"")
lsbsStr = "00001111000101100000010100001000"
% Convert the binary string to a decimal number
binaryNumber = bin2dec(lsbsStr)
binaryNumber = 253101320
For more information, please refer the shared MATLAB documentation links:
  1. fix: https://www.mathworks.com/help/matlab/ref/fix.html
  2. num2str : https://www.mathworks.com/help/matlab/ref/num2str.html
  3. join : https://www.mathworks.com/help/matlab/ref/join.html
I hope this helps.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!