HOW to convert negative integer to bit in Matlab?

Is there some function to do the job? thanks in advance

2 Comments

and is there some function that can help me convert decimal numbers into bits?
Please read this thread:
https://www.mathworks.com/matlabcentral/answers/98649-how-can-i-convert-a-negative-integer-to-a-binary-string-in-other-words-how-can-i-find-two-s-comple

Sign in to comment.

Answers (2)

Wel, you can use something like this (assuming you're working strictly with integers):
s = -10
s = -10
b = dec2bin(typecast(int8(s),'uint8'))
b = '11110110'
s_recovered = typecast(uint8(bin2dec(b)),'int8')
s_recovered = int8 -10
For a few years now, dec2bin can do it directly. For example
dec2bin(-11,8)

7 Comments

dec2bin(-11.8)
Error using dec2bin (line 33)
D must be a non-negative integer smaller than flintmax.
I have MATLAB 2018a, maybe that is the problem
Example:
number = randi([-99 -1])
number = -81
%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_unsigned = uint16 65455
as_bits = dec2bin(as_unsigned, 16)
as_bits = '1111111110101111'
Several of these steps can be combined of course.
as_bits = dec2bin(typecast(int16(number),'uint16'),16)
as_bits = '1111111110101111'
As there an alternative? Yes.
char( '1' + '0' - dec2bin(-number-1, 16))
ans = '1111111110101111'
Or you could take the shortcut that '1' + '0' works out to 97
Excuse me, I do not understand you quite well. I want binary number from -0.625 (a part from a number), not from whole number nor from negative whole number.
I apologise in advance if this is too easy or sth else, my understanding of this is small and maybe my english is little rusty.
Thank you for your time.
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.
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)
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.
thank you, Iwe already made my code

Sign in to comment.

Products

Release

R2018a

Asked:

on 6 Jan 2023

Commented:

on 9 Jan 2023

Community Treasure Hunt

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

Start Hunting!