Shop OBEX P1 Docs P2 Docs Learn Events
Large Numbers — Parallax Forums

Large Numbers

ArchiverArchiver Posts: 46,084
edited 2001-11-29 23:01 in General Discussion
Hello,

I'm controlling, serially, a device that sends me large 16 and 32 bit numbers
in a series of 8 bit packets. After I receive each number into a byte sized
variable, I think I can create 16 bit numbers by multiplying the first byte by
16 and then adding the second byte and storing that number into a word size
variable. Is this correct?

However, since word is the largest variable size on the stamp (i'm using a
bs2p), how do I combine four bytes to make a 32 bit variable that I can then do
math on (division mostly) ? Does anyone have experience dealing with large
numbers like this?

Thanks,
Stewart

--
Stewart Mayer, stewlist@k... on 11/29/2001

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-11-29 19:56
    Word1 = Byte1 * 256 + Byte0

    Or:

    Word1 = Byte1
    Word1 = Word1<<8 + Byte0

    Or:
    Word1.HighByte = Byte1
    Word1.LowByte = Byte 0


    That should work for 16 bits.

    Combining for 32 bits is a bit tricky. Sure, you can store 4 bytes in a
    byte array together. But the Stamp has no easy way to handle 32 bit
    math. I show some tricks in my Stamp book and Tracy has a lot on his Web
    site, but general purpose 32-bit math on the Stamp is a difficult
    proposition.

    Although it is probably overkill, consider a PAK-I, II, or IX :
    http://www.al-williams.com/awce/pak1.htm

    Good luck!

    Al Williams
    AWC
    * Control 8 servos at once
    http://www.al-williams.com/awce/pak8.htm

    >
    Original Message
    > From: Stewart Mayer [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=E22VRhWQHhwNBIryfOSMna9jBEJiwYkyR4h1i0cyxOteRGbJoICMzC4sv_bui9xJbLXyrc5M59WfWYG8XQo3FFPduA]stewlist@k...[/url
    > Sent: Thursday, November 29, 2001 1:39 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Large Numbers
    >
    >
    > Hello,
    >
    > I'm controlling, serially, a device that sends me large 16
    > and 32 bit numbers
    > in a series of 8 bit packets. After I receive each number
    > into a byte sized
    > variable, I think I can create 16 bit numbers by multiplying
    > the first byte by
    > 16 and then adding the second byte and storing that number
    > into a word size
    > variable. Is this correct?
    >
    > However, since word is the largest variable size on the stamp
    > (i'm using a
    > bs2p), how do I combine four bytes to make a 32 bit variable
    > that I can then do
    > math on (division mostly) ? Does anyone have experience
    > dealing with large
    > numbers like this?
    >
    > Thanks,
    > Stewart
    >
    > --
    > Stewart Mayer, stewlist@k... on 11/29/2001
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-29 20:06
    Unfortunately, as far as I know, the stamp has no 'carry' bit the way you
    would do it in most processors. I wonder if you could fake the high bit as
    a carry bit and use that to carry mathematical operations into a second
    byte? so 15 bits would be used as a representation from 0-32767, and the
    16th bit wouldn't represent anything except carry, which would end up in
    the second byte.

    On Thu, 29 Nov 2001, Al Williams wrote:

    > Word1 = Byte1 * 256 + Byte0
    >
    > Or:
    >
    > Word1 = Byte1
    > Word1 = Word1<<8 + Byte0
    >
    > Or:
    > Word1.HighByte = Byte1
    > Word1.LowByte = Byte 0
    >
    >
    > That should work for 16 bits.
    >
    > Combining for 32 bits is a bit tricky. Sure, you can store 4 bytes in a
    > byte array together. But the Stamp has no easy way to handle 32 bit
    > math. I show some tricks in my Stamp book and Tracy has a lot on his Web
    > site, but general purpose 32-bit math on the Stamp is a difficult
    > proposition.
    >
    > Although it is probably overkill, consider a PAK-I, II, or IX :
    > http://www.al-williams.com/awce/pak1.htm
    >
    > Good luck!
    >
    > Al Williams
    > AWC
    > * Control 8 servos at once
    > http://www.al-williams.com/awce/pak8.htm
    >
    > >
    Original Message
    > > From: Stewart Mayer [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=Wh-l41oKYA0UyEN3I6okCtat0-GI4Hl2QmmA10vjtb6-LvLoRAc3HPfqbllTfkAv-0INs0_yBQq8fovweD0cSFs]stewlist@k...[/url
    > > Sent: Thursday, November 29, 2001 1:39 PM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Large Numbers
    > >
    > >
    > > Hello,
    > >
    > > I'm controlling, serially, a device that sends me large 16
    > > and 32 bit numbers
    > > in a series of 8 bit packets. After I receive each number
    > > into a byte sized
    > > variable, I think I can create 16 bit numbers by multiplying
    > > the first byte by
    > > 16 and then adding the second byte and storing that number
    > > into a word size
    > > variable. Is this correct?
    > >
    > > However, since word is the largest variable size on the stamp
    > > (i'm using a
    > > bs2p), how do I combine four bytes to make a 32 bit variable
    > > that I can then do
    > > math on (division mostly) ? Does anyone have experience
    > > dealing with large
    > > numbers like this?
    > >
    > > Thanks,
    > > Stewart
    > >
    > > --
    > > Stewart Mayer, stewlist@k... on 11/29/2001
    > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed. Text in the
    > > Subject and Body of the message will be ignored.
    > >
    > >
    > > Your use of Yahoo! Groups is subject to
    > > http://docs.yahoo.com/info/terms/
    > >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >

    Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
    Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma - Bremerton
    email: lamont@a... WWW: http://www.serv.net
    "...There's no moral, it's just a lot of stuff that happens". - H. Simpson
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-29 20:18
    >Hello,
    >
    >I'm controlling, serially, a device that sends me large 16 and 32 bit numbers
    >in a series of 8 bit packets. After I receive each number into a byte sized
    >variable, I think I can create 16 bit numbers by multiplying the first byte by
    >16 and then adding the second byte and storing that number into a word size
    >variable. Is this correct?


    Hi Stewart,

    You can receive the bytes directly into words
    :
    wx var word
    wy var word
    z0 var wx.byte0
    z1 var wx.byte1
    z2 var wy.byte0
    z3 var wy.byte1

    serin dpin,baudmode,[noparse][[/noparse]z0,z1,z2,z3] ' 4 bytes

    That assuming that your device sends the least significant byte first.



    >
    >However, since word is the largest variable size on the stamp (i'm using a
    >bs2p), how do I combine four bytes to make a 32 bit variable that I
    >can then do
    >math on (division mostly) ? Does anyone have experience dealing with large
    >numbers like this?

    Check out http://www.emesystems.com/BS2math6.htm

    The methods of integer math have to be tailored to each situation,
    depending on the range of the variables.

    >
    >Thanks,
    > Stewart
    >
    >--
    >Stewart Mayer, stewlist@k... on 11/29/2001

    -- best regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...
  • ArchiverArchiver Posts: 46,084
    edited 2001-11-29 23:01
    Thanks for the tip on receiving words. I think I'll try to come up with a
    mathmatical means to work with the large numbers.

    stew

    On Thu, 29 Nov 2001 12:18:20 -0800, Tracy Allen wrote:
    >>Hello,
    >>
    >>I'm controlling, serially, a device that sends me large 16 and 32 bit numbers
    >>in a series of 8 bit packets. After I receive each number into a byte sized
    >>variable, I think I can create 16 bit numbers by multiplying the first byte
    by
    >>16 and then adding the second byte and storing that number into a word size
    >>variable. Is this correct?
    >
    >
    >Hi Stewart,
    >
    >You can receive the bytes directly into words
    >:
    >wx var word
    >wy var word
    >z0 var wx.byte0
    >z1 var wx.byte1
    >z2 var wy.byte0
    >z3 var wy.byte1
    >
    >serin dpin,baudmode,[noparse][[/noparse]z0,z1,z2,z3] ' 4 bytes
    >
    >That assuming that your device sends the least significant byte first.
    >
    >
    >
    >>
    >>However, since word is the largest variable size on the stamp (i'm using a
    >>bs2p), how do I combine four bytes to make a 32 bit variable that I
    >>can then do
    >>math on (division mostly) ? Does anyone have experience dealing with large
    >>numbers like this?
    >
    >Check out http://www.emesystems.com/BS2math6.htm
    >
    >The methods of integer math have to be tailored to each situation,
    >depending on the range of the variables.
    >
    >>
    >>Thanks,
    >>Stewart
    >>
    >>--
    >>Stewart Mayer, stewlist@k... on 11/29/2001
    >
    >-- best regards
    >Tracy Allen
    >electronically monitored ecosystems
    >http://www.emesystems.com
    >mailto:tracy@e...
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    >basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and Body
    of the message will
    >be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >


    --
    Stewart Mayer, stewlist@k... on 11/29/2001
Sign In or Register to comment.