Intel Pentium Processor data format for negative number

Discussion in 'C Programming' started by Subrahmanyam Arya, Jul 28, 2003.

  1. Hi Folks ,

    I am trying to solve the problem of reading the numbers correctly
    from a serial line into an intel pentium processor machine .

    I am reading 1 byte and 2byte data both positive and negative
    quantities .

    Can any one tell me what are the things i should bear in mind .

    I did some small c-program tests and found out that the intel
    processor uses
    signed 2's complement for representing negative numbers .

    The data coming over the serial line is from some third party devices
    ..

    I am assuming that the positive numbers are always in signed magitude
    form ,
    so no special care needs to be taken except for byte ordering . IS
    this right .

    Where as for negative numbers we need to know in what format the data
    is coming
    and convert into signed 2's complement form so that the rest of the
    math or type
    casting to some other types for example int or float etc will be
    correct .

    Is this correct .

    Am i right when i said the negative numbers are only in 2's complement
    form in
    intel machines.

    I am thinking the same applies wether it is 1 byte , 2 byte or 4 byte
    quantities. IS this right .


    We are running QNX OS , but i guess this problem is not related to the
    OS .

    Please give me some right directions , alert me to the pitfalls here .


    Thanks in advance
    -Subra
     
    Subrahmanyam Arya, Jul 28, 2003
    #1
    1. Advertising

  2. > I'm sure that QNX would adopt the native hardware integer format of any
    > machine it runs on. I don't know of any current architecture which is
    > supported as a target for C compilers, which uses other than 2's complement
    > signed integer types. The trend was set in stone before Intel came into
    > the CPU business.
    >
    > You could check whether the various signed integer types are all 2's
    > complement; it would be strange if it were not so. If you're thinking of
    > relying on the low order bit patterns being the same for various sized
    > integers, the compiler knows all it needs about that, so there's no point
    > in writing code at that low a level.
    >
    > I don't care to guess why you might not consider unsigned integer types to
    > be positive.



    The problem i have is in reading the data over a serial line . When
    the third party vendor is sending a 2 byte data over a serial line for
    a signed quantity .
    He might not be sending it in a 2's complement form . In that case
    when i read into a signed short i need to convert into a 2's
    complement and then assign it to the signed short , only then my c -
    compiler would interpret correctly .
    I guess this is where i am trying to find out what i was thinking is
    right or not .

    best regards
    -subra
     
    Subrahmanyam Arya, Jul 29, 2003
    #2
    1. Advertising

  3. >The problem i have is in reading the data over a serial line . When

    Then the problem you have involves the data format OVER THE SERIAL
    LINE. For example, one way of sending a 16-bit number over a serial
    line involves sending an ASCII '-' character if the number is
    negative, followed by one or more ASCII digits in whatever the
    agreed-upon base is (typically decimal or hexadecimal), followed
    by a terminator/separator character such as an ASCII space, comma,
    or newline.

    >the third party vendor is sending a 2 byte data over a serial line for
    >a signed quantity .
    >He might not be sending it in a 2's complement form . In that case


    Then you need to know what form he is sending it in, and that need
    not have ANYTHING to do with the native hardware on either end of
    the connection. And depending on how he says it, "2 byte data"
    does not necessarily mean 2 BYTES ON THE SERIAL LINE. It may
    simply mean "a 16-bit quantity".

    >when i read into a signed short i need to convert into a 2's
    >complement and then assign it to the signed short , only then my c -
    >compiler would interpret correctly .
    >I guess this is where i am trying to find out what i was thinking is
    >right or not .


    Gordon L. Burditt
     
    Gordon Burditt, Jul 29, 2003
    #3
  4. Subrahmanyam Arya wrote:

    > I am trying to solve the problem of reading the numbers correctly
    > from a serial line into an Intel Pentium processor machine.
    >
    > I am reading 1 byte and 2byte data both positive and negative
    > quantities .
    >
    > Can any one tell me what are the things that I should bear in mind.
    >
    > I did some small C-program tests and found out that
    > the Intel processor uses 2's complement to represent integral numbers.
    >
    > The data coming over the serial line is from some third party device.
    >
    > I am assuming that
    > the positive numbers are always in signed magnitude form,
    > so no special care needs to be taken except for byte ordering.
    > Is this correct.
    >
    > Where as, for negative numbers,
    > we need to know in what format the data is coming
    > and convert into signed 2's complement form
    > so that the rest of the math or type casting to some other types
    > for example int or float etc will be correct.
    >
    > Is this correct?
    >
    > Am I right when I said that the negative numbers
    > are only in 2's complement form in Intel machines.
    >
    > I am thinking the same applies whether it is 1 byte, 2 byte or 4 byte
    > quantities. IS this right .
    >
    > We are running QNX OS,
    > but I guess this problem is not related to the OS.
    >
    > Please give me some right directions, alert me to the pitfalls here.


    You are completely confused.
    You got almost everything wrong.
    Intel uses a *2's complement* representation
    for both positive and negative signed fixed-point (integral) types.
    Intel uses a *sign and magnitude* representation
    for both positive and negative floating-point types.
    This is true
    for most other general purpose computer architectures as well.
    Intel uses a *little endian* *byte ordering* --
    the bytes are stored in memory in order from least to most significant.
    Other processors (the PowerPC for example)
    use *big endian* *byte ordering* -- the bytes are stored in memory
    in reverse order from most to least significant.
    Usually, you only need to consider the *byte ordering*
    when transferring signed or unsigned fixed-point (integral) types
    from one architecture to another.
    Intel uses the standard IEEE 754 format for floating-point types.

    The ANSI/ISO C standard has *no* provisions for "type casting"
    a representation of either fixed-point or floating-point numbers
    from one machine to an equivalent representation on another machine.
    You will be obliged to write or purchase machine specific software
    to implement any such conversions unless the representations
    are identical on both machines.
     
    E. Robert Tisdale, Jul 29, 2003
    #4
  5. "E. Robert Tisdale" <> wrote in message news:<>...
    > Subrahmanyam Arya wrote:
    >
    > > I am trying to solve the problem of reading the numbers correctly
    > > from a serial line into an Intel Pentium processor machine.
    > >
    > > I am reading 1 byte and 2byte data both positive and negative
    > > quantities .
    > >
    > > Can any one tell me what are the things that I should bear in mind.
    > >
    > > I did some small C-program tests and found out that
    > > the Intel processor uses 2's complement to represent integral numbers.
    > >
    > > The data coming over the serial line is from some third party device.
    > >
    > > I am assuming that
    > > the positive numbers are always in signed magnitude form,
    > > so no special care needs to be taken except for byte ordering.
    > > Is this correct.
    > >
    > > Where as, for negative numbers,
    > > we need to know in what format the data is coming
    > > and convert into signed 2's complement form
    > > so that the rest of the math or type casting to some other types
    > > for example int or float etc will be correct.
    > >
    > > Is this correct?
    > >
    > > Am I right when I said that the negative numbers
    > > are only in 2's complement form in Intel machines.
    > >
    > > I am thinking the same applies whether it is 1 byte, 2 byte or 4 byte
    > > quantities. IS this right .
    > >
    > > We are running QNX OS,
    > > but I guess this problem is not related to the OS.
    > >
    > > Please give me some right directions, alert me to the pitfalls here.

    >
    > You are completely confused.


    I am not sure why ? .

    > You got almost everything wrong.
    > Intel uses a *2's complement* representation
    > for both positive and negative signed fixed-point (integral) types.
    > Intel uses a *sign and magnitude* representation
    > for both positive and negative floating-point types.>

    This is true for most other general purpose computer architectures as
    well.


    I know that negative integers use signed 2's complement and positive
    use signed
    magnitude . You are telling me that positive integeers use signed 2's
    complement i don't think so , i will double check on this .



    > Intel uses a *little endian* *byte ordering* --
    > the bytes are stored in memory in order from least to most significant.
    > Other processors (the PowerPC for example)
    > use *big endian* *byte ordering* -- the bytes are stored in memory
    > in reverse order from most to least significant.
    > Usually, you only need to consider the *byte ordering*
    > when transferring signed or unsigned fixed-point (integral) types
    > from one architecture to another.
    > Intel uses the standard IEEE 754 format for floating-point types.
    >


    I am aware of this .


    > The ANSI/ISO C standard has *no* provisions for "type casting"
    > a representation of either fixed-point or floating-point numbers
    > from one machine to an equivalent representation on another machine.
    > You will be obliged to write or purchase machine specific software
    > to implement any such conversions unless the representations
    > are identical on both machines.


    I am planning to write this routines , it is not two machines , one is
    a small embedded device with some microcontroller and some I/O on it ,
    the other is intel -pentium processor based PC . My problem is the
    analog inputs sensed by the i/o is put on the serial line by
    microcontroller in bigendian format , so i have to convert into little
    indian apart from that i need to see if the negative numbers are put
    in signed 2's complement so that i don't have to do conversion for
    this .

    This is where i also think that positive integers are put in signed
    magnitude form on the wire and i only need to bother about byte
    ordering . Ofcourse i need to verify the fact that the intel uses
    signed magnitude for positive integers .


    I hope i am more clear
    appreciate your thoughts
    -subra
     
    Subrahmanyam Arya, Jul 30, 2003
    #5
  6. Subrahmanyam Arya

    Richard Bos Guest

    (Subrahmanyam Arya) wrote:

    > "E. Robert Tisdale" <> wrote in message news:<>...
    > > Subrahmanyam Arya wrote:
    > >
    > > > I am trying to solve the problem of reading the numbers correctly
    > > > from a serial line into an Intel Pentium processor machine.


    [ Snip! ]

    > > > I am thinking the same applies whether it is 1 byte, 2 byte or 4 byte
    > > > quantities. IS this right .

    > >
    > > You are completely confused.

    >
    > I am not sure why ? .


    Neither am I, but the symptoms are clear. You're completely confused on
    signed and unsigned integers.

    > > You got almost everything wrong.
    > > Intel uses a *2's complement* representation
    > > for both positive and negative signed fixed-point (integral) types.
    > > Intel uses a *sign and magnitude* representation
    > > for both positive and negative floating-point types.>

    >
    > I know that negative integers use signed 2's complement and positive
    > use signed magnitude .


    I'm sorry, but there's only one description for this sentence: you're
    talking out of your arse.

    _Signed_ integers use _either_ 2's complement _or_ 1's complement _or_
    sign magnitude. There cannot be any difference between the positive and
    negative values of a signed integer in this respect, since the
    difference between those methods is _how a negative number is
    represented_. Positive values are identical for all three methods.

    > You are telling me that positive integeers use signed 2's
    > complement i don't think so , i will double check on this .


    No, he's telling you that _signed integers_ on an Intel machine use 2's
    complement. Positive _and_ negative.

    > > The ANSI/ISO C standard has *no* provisions for "type casting"
    > > a representation of either fixed-point or floating-point numbers
    > > from one machine to an equivalent representation on another machine.
    > > You will be obliged to write or purchase machine specific software
    > > to implement any such conversions unless the representations
    > > are identical on both machines.

    >
    > I am planning to write this routines , it is not two machines , one is
    > a small embedded device with some microcontroller and some I/O on it ,
    > the other is intel -pentium processor based PC .


    So those are not two machines? What are they then, modern sculptures in
    the medium of dirty silicon?

    > My problem is the
    > analog inputs sensed by the i/o is put on the serial line by
    > microcontroller in bigendian format , so i have to convert into little
    > indian


    One little, two little, three little indians... sorry <g>. Perhaps a
    natural typo, with yout name...

    > apart from that i need to see if the negative numbers are put
    > in signed 2's complement so that i don't have to do conversion for
    > this .


    Have the device send a -1 and see how it does it. Do not bother with the
    positive numbers, since it makes no difference to those.

    > This is where i also think that positive integers are put in signed
    > magnitude form on the wire


    Look, I'll draw you a table, ok?

    Value| 2's somp | 1's comp | sign magn
    -----+----------+----------+-----------
    0 | 00000000 | 00000000 | 00000000
    (-0)| -- | 11111111 | 10000000
    1 | 00000001 | 00000001 | 00000001
    -1 | 11111111 | 11111110 | 10000001
    2 | 00000010 | 00000010 | 00000010
    -2 | 11111110 | 11111101 | 10000010
    3 | 00000011 | 00000011 | 00000011
    -3 | 11111101 | 11111100 | 10000011
    14 | 00001110 | 00001110 | 00001110
    -14 | 11110010 | 11110001 | 10001110
    127 | 01111111 | 01111111 | 01111111
    -127 | 10000001 | 10000000 | 11111111
    -128 | 10000000 | -- | --

    Note the complete lack of difference between the methods where positive
    numbers are concerned.

    > and i only need to bother about byte
    > ordering .


    Indeed. To figure _that_ one out, have the device send the hex number
    0x1234, and see if it comes through as 0x12, 0x34 or 0x34, 0x12. Then,
    to make sure that it's actually big- or little-endian, and not something
    muddled in between (such devices exist, oddly enough), send a four-byte
    value and check that it comes back as you expect it.

    > Ofcourse i need to verify the fact that the intel uses
    > signed magnitude for positive integers .


    You will never verify that "fact", since it's not a fact, it's bollocks.

    Richard
     
    Richard Bos, Jul 30, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Allen
    Replies:
    1
    Views:
    374
    arkam
    Apr 14, 2004
  2. arkam
    Replies:
    0
    Views:
    407
    arkam
    Apr 13, 2004
  3. Alvin Bruney [MVP]
    Replies:
    1
    Views:
    461
    arkam
    Apr 14, 2004
  4. whatnext
    Replies:
    9
    Views:
    437
  5. Kazik�
    Replies:
    4
    Views:
    1,366
    Jonathan Lee
    Jul 6, 2009
Loading...

Share This Page