Thread Subject: negative binary numbers

Subject: negative binary numbers

From: mut

Date: 4 Feb, 2008 19:53:02

Message: 1 of 6

hi all:

can anyone tell me how to get binary negative numbers with
matlab.

i recall that these numbers are tricky, the most significant
bit is the sign bit. so for a 8 bit binary number -3 should be

10000011

i've tried with dec2bin and bin2dec (to get it back to
decimal). i've read dec2bin help and it just works for
nonnegative numbers ... any advice???

thanks in advance

Subject: negative binary numbers

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Feb, 2008 20:04:48

Message: 2 of 6

In article <fo7qeu$dv4$1@fred.mathworks.com>, mut <mutante51@gmail.com> wrote:

>can anyone tell me how to get binary negative numbers with
>matlab.

>i recall that these numbers are tricky, the most significant
>bit is the sign bit. so for a 8 bit binary number -3 should be

>10000011

>i've tried with dec2bin and bin2dec (to get it back to
>decimal). i've read dec2bin help and it just works for
>nonnegative numbers ... any advice???

Two's complement:

>> dec2bin(typecast(int8(-3),'uint8'))


ans =

11111101


Sign-magnitude:

>> dec2bin(128 + typecast(abs(int8(-3)),'uint8'))
ans =

10000011


One's complement:

>> dec2bin(typecast(int8(-3),'uint8')-1)

ans =

11111100

(Yes, it is correct that -0 would become 11111111 in this scheme.)
--
   "I was very young in those days, but I was also rather dim."
   -- Christopher Priest

Subject: negative binary numbers

From: Bruno Luong

Date: 4 Feb, 2008 20:08:03

Message: 3 of 6

The only think you need to remember is -1 is coded in n-bit
binary as:
-1 -> 111...111

ans succesively:

-2 -> 111...110
-3 -> 111...100

And you can go down to -2^(n-1) in

-2^(n-1) -> 100...000

Bruno

Subject: negative binary numbers

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Feb, 2008 20:23:16

Message: 4 of 6

In article <fo7rb2$qk7$1@fred.mathworks.com>,
Bruno Luong <b.luong@fogale.fr> wrote:
>The only think you need to remember is -1 is coded in n-bit
>binary as:
>-1 -> 111...111

There are 2^n ways to code -1 in n-bit binary and there are
at least 2^n * n! ways to code the integers down to -2^(n-1)
in n-bit binary.

The particular scheme the original poster used, -3 -> 10000011
occurs in at least (n-1)! of the ways, and in particular occurs
in "signed-magnitude" (or "sign and magnitude") systems.
It is a well-known system, but not one in general use.

Whether the original poster wanted signed-magnitude or
two's-complement is a different question...
--
   "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson

Subject: negative binary numbers

From: Bruno Luong

Date: 4 Feb, 2008 20:32:02

Message: 5 of 6

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fo7s7k$s0m$1@canopus.cc.umanitoba.ca>...

>
> There are 2^n ways to code -1 in n-bit binary and there are
> at least 2^n * n! ways to code the integers down to -2^(n-1)
> in n-bit binary.
>

Original poster wrote:

> can anyone tell me how to get binary negative numbers with
> *matlab*.

Humm OK, may be I miss understood something, but to me there
is only one way binary numbers are coded within *MATLAB*.

Bruno

Subject: negative binary numbers

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Feb, 2008 20:55:25

Message: 6 of 6

In article <fo7so2$g55$1@fred.mathworks.com>,
Bruno Luong <b.luong@fogale.fr> wrote:

>Humm OK, may be I miss understood something, but to me there
>is only one way binary numbers are coded within *MATLAB*.

Leaving out the Fixed Point Toolbox, and the Symbolic and Maple
toolboxes, there are three distinct
ways that binary numbers are coded within Matlab:

  - integer (int8, int16, int32, int64)
  - single precision floating point, 32 bit (single)
  - double precision floating point, 64 bit (double)

Double precision is quite similar to the (now standardized) single
precision, but does not merely use wider fields: it also uses
a larger exponent offset in the mantissa.


But the question wasn't really about internal representations:
it was about forming an external representation as a string
of '0' and '1' characters. Forming an external representation
is often to meet the needs of some external interface, so the way
that that external interface wants negative numbers to be coded
becomes the important part, rather than the way that Matlab itself
encodes them.
--
   "I was very young in those days, but I was also rather dim."
   -- Christopher Priest

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
binary mut 4 Feb, 2008 14:54:59
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com