MSB after converting decimal values to binary

7 views (last 30 days)
I've used this code for converting decimal values to binary. But I cannot understand how it's working. I need to find most significant bits. I mean which bits have more effect if error happened to them?
m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[]);
b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b));
b=-1.12;
m=1110110001010001101110000001111010000101111010111111000110111111;

Answers (2)

Walter Roberson
Walter Roberson on 27 Feb 2017
The typecast() to uint8 is going to get you a sequence of byte values.
To interpret that sequence of byte values, you need to know whether your system is "Big Endian" or "Little Endian". Big Endian systems have the most significant byte first in memory; Little Endian systems have the most significant byte last in memory. All x86 and x64 architectures are Little Endian. (MATLAB is not presently supported on any Big Endian systems.)
You are almost certainly using a little endian system. So it is the last of the bytes that has the most significant byte. That will come out as the last chunk of 8 characters in m. Out of that chunk of 8, the first of them is the most significant bit of the byte. The most significant bit of the most significant byte would thus be at m(end-7)
  10 Comments
Walter Roberson
Walter Roberson on 2 Mar 2017
In the original context that I wrote the typecast code for, that then got posted here, the size of the data was unknown until execution time... it comes from wavread() or audioread() with the 'native' parameter.
I suspect Rihanna is needing to do something like audio steganography.
Guillaume
Guillaume on 2 Mar 2017
Ok. I was missing the context.
However, if you just have a string of bytes, asking which bit is most significant is kind of meaningless, if you don't know what these bytes represent. So, at some point, the size and type of the data must be known.

Sign in to comment.


Guillaume
Guillaume on 27 Feb 2017
As Walter explained, the code converts a double into its binary representation. Due to the way doubles are encoded there are a lot of important bits. Which "bits have more effect if error happened to them" is subjective to what the number represent. Changing bit 64 only changes the sign, the magnitude stays the same. Changing bit 63 to 53 will greatly change the magnitude of the number, while bits 52 to 1 will only affect the value of the number.
Here is a simple code that shows of effect of swapping each byte in turn:
%function to invert the bit of a double value:
bitnot = @(dval, bit) typecast(bitset(typecast(dval, 'uint64'), bit, ~bitget(typecast(dval, 'uint64'), bit)), 'double');
number = -1.12; %number to test on
array2table([1:64; bitnot(number, 1:64)]', 'VariableNames', {'swapped_bit', 'result'})
  4 Comments
Guillaume
Guillaume on 2 Mar 2017
Edited: Guillaume on 2 Mar 2017
As discussed in Walter's answer, this is because I look at the bits as one lump of 64 bits. Yes, if you look at how these bits are arranged in memory in a sequential way, that bit may end up at the 57th position but for the processor, this is still bit 64.
In any case, the code does show you which bit does what, using the standard bit numbering convention (using base 1) that is used in any article discussing IEE754 (such as the wikipedia page).
ghibeche
ghibeche on 30 Dec 2022
Hi
I want function like uint64 but generate 56 bit

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!