melisa zand
melisa zand

Reputation: 211

how matlab pack negative number as int16

how matlab pack negative number as int16 like -22 ?

for positive number I notice that when we have 4 digits number it pack it like this :

the numbers is : 1254 matlab pack it as 230 and 4 then if you convert these numbers to binary we will have 11100110 for 230 and 100 for 4 . now you can convert it to normal format like this :

>> bin2dec('10011100110')

ans =

       1254.00

now how it works for negative number ? ( when it packed -22 to int16 it convert to 234 and 255 )

Upvotes: 1

Views: 3095

Answers (1)

Amro
Amro

Reputation: 124563

Here is how to do the conversion in both directions:

%# convert from int16 to binary
>> dec2bin(typecast(int16(-22),'uint16'))
ans =
1111111111101010

%# convert from binary representation to int16
>> typecast(uint16(bin2dec('1111111111101010')),'int16')
ans =
    -22

Upvotes: 3

Related Questions