필터 지우기
필터 지우기

MSB after converting decimal values to binary

조회 수: 7 (최근 30일)
Rihanna Waterson
Rihanna Waterson 2017년 2월 27일
댓글: ghibeche 2022년 12월 30일
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;

답변 (2개)

Walter Roberson
Walter Roberson 2017년 2월 27일
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
Walter Roberson
Walter Roberson 2017년 3월 2일
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 2017년 3월 2일
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.

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2017년 2월 27일
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
Guillaume
Guillaume 2017년 3월 2일
편집: Guillaume 2017년 3월 2일
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 2022년 12월 30일
Hi
I want function like uint64 but generate 56 bit

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by