HOW to convert negative integer to bit in Matlab?
Show older comments
Answers (2)
Wel, you can use something like this (assuming you're working strictly with integers):
s = -10
b = dec2bin(typecast(int8(s),'uint8'))
s_recovered = typecast(uint8(bin2dec(b)),'int8')
Walter Roberson
on 6 Jan 2023
For a few years now, dec2bin can do it directly. For example
dec2bin(-11,8)
7 Comments
Zoran
on 6 Jan 2023
Example:
number = randi([-99 -1])
%this is represented as double precision. For the following steps we need
%integer data type
as_integer = int16(number);
as_unsigned = typecast(as_integer, 'uint16')
as_bits = dec2bin(as_unsigned, 16)
Several of these steps can be combined of course.
as_bits = dec2bin(typecast(int16(number),'uint16'),16)
As there an alternative? Yes.
char( '1' + '0' - dec2bin(-number-1, 16))
Or you could take the shortcut that '1' + '0' works out to 97
Zoran
on 7 Jan 2023
Walter Roberson
on 7 Jan 2023
When you start from a (possibly negative) integer (not floating point) and want to convert to binary, there are only a small number of standard representations in binary.
- you can reserve one bit explicitly to indicate whether the number is negative or not (by convention negative is signalled by using 1 for the sign bit.) Then the remaining bits are the binary representation of the absolute value of the number. If you use this scheme you can have "negative zero". This scheme is called separate sign
- you can subtract the lowest desired integer from the value, and represent the resulting non-negative result in binary. so for example if you wanted to represent down to -31 you would subtract -31 from each input (which is to say, add 31). In practice this scheme is almost never used for mantissa, but it does get used for exponents. This scheme is called "Excess-127" or similar notation where 127 is the value being added
- you can flip each bit of the number to calculate the negative. 0110 (6) would become 1001 for -6. This scheme also permits negative zero. This does not get used nearly as much as the next scheme. This scheme is called "One's Complement" and is mostly used for error detection purposes and the occasional industrial machine, but not so much.
- you can flip each bit and then add binary 1 to the result. 0110 flipped to 1001, add 1, result 1010 for -6. This scheme does not permit negative zero. However this scheme has very nice computation properties. This is called "two's complement" and is by far the most popular for integers.
Walter Roberson
on 8 Jan 2023
So there are four possible ways to represent negative integers in binary. By far the most common way is two's complement, but separate sign does get used (for example, Fixed Point Toolbox), and one's complement does get used sometimes; "excess-" is seldom used.
For nearly all practical purposes, you would just need to know whether the representation is "two's complement" or "separate sign" (but one's complement does show up from time to time)
Walter Roberson
on 8 Jan 2023
So there are very small number of representations used for negative integers, and two of those together cover the great majority of the cases.
But when it comes to binary numbers that include negative fractions then the only common representations are IEEE 754 Binary Single Precision (a 32 bit representation) and IEEE 754 Binary Double Precision (a 64 bit representation.) There are a couple of other special-purpose representations, such as Intel's 80-bit Extended Precision. IBM uses a slightly different representation for its GPUs. There is an IEEE-754 standard for "Half Precision" (a 16 bit representation) which MATLAB implements as half which is not used much outside of Deep Learning.
Other than the Deep Learning "half precision" context, people who want negative binary fractions typically use custom fixed-point formats, typically using separate sign, and a fixed number of bits for before the decimal place, and a fixed number of bits for after the decimal place. Applications in which you need a "small" binary fraction often have very specific dynamic ranges that they need to operate over, for which it does not make sense to use a standardized representation such as "half precision"
Because of this... before we can tell you how to convert negative fractional values to binary, you need to define for us exactly what binary representation you want to use.
Zoran
on 9 Jan 2023
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!