Convey binary sting into 2 bits format

I had an binay values file in matrix format . Eg [101101110111010, 111111111111111,010011110101111] . I want to seperate 15bits each into 2 bits and write it in other file in decimal format

5 Comments

I want to seperate 15bits each into 2 bits and write it in other file in decimal format
And what do you want to do with the bit that's left over? 15 bits is divisible into seven groups of 2 bits plus one group of 1 bit.
What is the exact form of your data? Do you have it as text:
T = '101101110111010'
T = '101101110111010'
Do you have it as numbers:
d = [1,0,1,1,0,1,1,1,0,1,1,1,0,1,0] % or
d = 1×15
1 0 1 1 0 1 1 1 0 1 1 1 0 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
L = logical(d)
L = 1x15 logical array
1 0 1 1 0 1 1 1 0 1 1 1 0 1 0
Do you have it as one number:
n = polyval(d, 2)
n = 23482
check = bitand(n, 2.^(14:-1:0)) ~= 0
check = 1x15 logical array
1 0 1 1 0 1 1 1 0 1 1 1 0 1 0
Or do you have it stored in some other format (and if so, please provide more details)?
If you have it as 101101110111010 then you are going to have trouble converting it to binary. If you must code it as 101101110111010 then code it as 0b101101110111010 to signal to the parser that it is binary.
Siva
Siva on 9 Oct 2024
Edited: DGM on 9 Oct 2024
Eg: 11111111111111111111110000011120101111000111100000 (48 bytes)
output should be spLit this into op: 11 11 11 11 11 11 11 11 11 11 ••••••• so on
Used reshape module.
The output should be written in other file
You haven't said how this data is stored in MATLAB. The answer will be different if you have it stored as a vector containing just 0 and 1 values, a vector containing false and true values, a vector containing the characters '0' and/or '1', an integer value where you expect to perform bitwise operations to decompose it into individual bits, a double precision value (in which case what are the 16 bits that you're not showing us supposed to represent -- a double is stored as 64 bits), or something else entirely.
I'm going to assume the 2 in your example was just a typo.
We also don't really know what the output is either.
Are we to assume it's just a text file? If it's supposed to be "decimal format", does that mean it's all a bunch of integers from 0 to 3?
xdec = 25831; % decimal
xbin = dec2bin(xdec,16); % char representation of binary
xbin = reshape(xbin,2,[]).'; % reshaped
xout = bin2dec(xbin).' % each 2b chunk is converted to decimal?
xout = 1×8
1 2 1 0 3 2 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
... or does that mean that the output is a bunch of decimal 0s and 1s in a file?
  1. How are the numbers represented in MATLAB?
  2. Are the numbers odd width? If yes, to what width should they be padded? 16? 48?
  3. Are these signed or unsigned values?
  4. Should they be converted into "decimal format" as shown?
  5. How is the output file to be formatted? CSV? Text? Something else? Delimiters? Be specific.
This is your question. It's up to you to say what you want.

Sign in to comment.

Answers (1)

11111111111111111111110000011120101111000111100000 (48 bytes)

MATLAB is going to treat this as a decimal number, in double precision. Unfortunately double precision numbers only get up to roughly 10^15 (15 digits) before they start to lose precision. Only roughly the first 15 digits will be preserved ,and the rest will be the closest representable number.

There is no way around this problem as long as you insist on double precision representation.

If you were to instead code

0b11111111111111111111110000011110101111000111100000

then matlab would know to use a binary representation, in this case a uint64 number. The limit however would be 63 or 64 bits of data.

1 Comment

In particular,
T = 11111111111111111111110000011110101111000111100000
T = 1.1111e+49
fprintf("%.999g\n", T)
11111111111111110805019569335803527359330256945152
T1 = 0b11111111111111111111110000011110101111000111100000
T1 = uint64 1125899646464480
fprintf("%.999g\n", T1)
1125899646464480
dec2bin(T1)
ans = '11111111111111111111110000011110101111000111100000'

Sign in to comment.

Tags

Asked:

on 7 Oct 2024

Edited:

on 9 Oct 2024

Community Treasure Hunt

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

Start Hunting!