First chance I've had to do a bit of tech stuff in the past 3 weeks, or look at vrf seriously. Warren's analysis is pretty good. But there's a bit more to say, formalize the knowledge a bit, so although this is in response to an old posting, here goes:

- another way to look at 2s complement is that the leftmost bit has a weight that is the _negative_ of its value based on bit position.

E.g. bit 0 has value 2^0==1, bit 1 has value 2^1==2, etc. In a UINT32, bit 31, the leftmost, would have value 2^31, but in an INT32 (2s complement), it has value -(2^31). To get the value of the number, add the weights of all its bits together.

E.g. start with a 1 followed by all 0s (100...0). Value is -(2^31) per the discussion above.

Now add 1, so that bits 31 and bit 0 are set, 100...01. This is -(2^31)+1.

Keep adding 1 till you get a result with all bits set to 1. Value is -(2^31)+2^30+2^29...+2^0==-1.

Add 1 one more time, and all the bits ripple to 0, because -1+1==0.

As you might guess, the rules for reporting overflow errors are a bit different than for straight binary.

- to find the negative of a twos-complement number, complement all the bits and add 1.

So for example, start with -1 = 111...11 as above. Complement all bits to get 000...0. Now add 1, giving 000...1, correct answer.

Now see if we can get back to -1. Complement all bits of 000...1, getting 111...0, then add 1, giving 111...1, the correct bits for -1.

Works whether the starting number is negative, positive, or 0.

Sorry, couldn't resist.

Best Regards,

Scott Bayes

Software Technical Support

Agilent Technologies, Inc.

815 14th Street S.W.

Loveland, CO, U.S.A. 80537

970 679 3799 Tel

970 635 6867 Fax

> -----Original Message-----

> From: Kathy Kiessling [mailto:k.kiessling@attbi.com]

> Sent: Wednesday, November 13, 2002 12:21 PM

> To: VEE vrf

> Subject: [vrf] Re: bits(str) function

>

>

> Here's bit more detailed explanation of the 2's complement

> representation:

>

> http://www.evergreen.edu/biophysics/tec ... s_comp.htm

>

> Kathy Kiessling

>

> ----- Original Message -----

> From: "Pickles, Warren" <Warren.Pickles@tycoelectronics.com>

> To: "VEE vrf" <vrf@it.lists.it.agilent.com>

> Sent: Wednesday, November 13, 2002 7:56 AM

> Subject: [vrf] bits(str) function

>

>

> > I've just been trying to to get to grips with the bits(str)

> function.

> > Thought I would share what I found, because it's not in the

> help file.

> >

> > According to the help, the bits(str) function "returns an

> Int32 with the

> > value of the digits in the Text str, converted to Int32 as base 2"

> >

> > So eg bits("011") returns 3. Fine.

> >

> > If you give bits(str) a string greater than 32 characters long, or

> > containing characters other than 0 or 1, it errors. Fine,

> because that

> > string definitely cannot be represented as Int32.

> >

> > But there are some strings containing 32 characters, all 0

> or 1, that

> can't

> > be represented as Int32, and then bits(str) doesn't give an

> error but

> > behaves rather strangely:

> >

> > If you give it a 0 followed by 31 1's:

> > bits("01111111111111111111111111111111") returns

> 2147483647. That's the

> > expected return value, and it is the highest possible

> Int32, ie 2^31-1

> >

> > If you "add" 1 to the input string:

> > bits("10000000000000000000000000000000") returns

> -2147483648. That's the

> > lowest possible Int32, ie -2^31. The return value ideally

> ought to be

> +2^31

> > but that is outside the range of Int32.

> >

> > "Add" 1 again to the input string:

> bits("10000000000000000000000000000001")

> > returns -2147483647. And if you keep "adding" 1 to the

> input value, it

> > looks like bits(str) adds one to the return value, until you get to

> > bits("11111111111111111111111111111111") which returns -1.

> >

> > It looks like this is what happens:

> > If the last character is 1 then add 1 to the return value.

> > If the last but one character is 1 then add 2 to the return value.

> > If the last but N character is 1 then add 2^N to the return value.

> > Except if N==31, when you subtract 2^31 instead of add.

> >

> > Regards,

> > Warren Pickles

> >

> > <<BitsStr.vee>>

> >

>

>

> --------------------------------------------------------------

> --------------

> ----

>

>

> > ---

> > You are currently subscribed to vrf as: k.kiessling@attbi.com

> > To subscribe send a blank email to

> "join-vrf@it.lists.it.agilent.com".

> > To unsubscribe send a blank email to

> "leave-vrf@it.lists.it.agilent.com".

> > To send messages to this mailing list, email

> "vrf@it.lists.it.agilent.com".

> > If you need help with the mailing list send a message to

> "owner-vrf@it.lists.it.agilent.com".

> >

>

>

> ---

> You are currently subscribed to vrf as: scott_bayes@agilent.com

> To subscribe send a blank email to "join-vrf@it.lists.it.agilent.com".

> To unsubscribe send a blank email to

> "leave-vrf@it.lists.it.agilent.com".

> To send messages to this mailing list, email

> "vrf@it.lists.it.agilent.com".

> If you need help with the mailing list send a message to

> "owner-vrf@it.lists.it.agilent.com".

>

---

You are currently subscribed to vrf as: rsb@soco.agilent.com

To subscribe send a blank email to "join-vrf@it.lists.it.agilent.com".

To unsubscribe send a blank email to "leave-vrf@it.lists.it.agilent.com".

To send messages to this mailing list, email "vrf@it.lists.it.agilent.com".

If you need help with the mailing list send a message to "owner-vrf@it.lists.it.agilent.com".

- another way to look at 2s complement is that the leftmost bit has a weight that is the _negative_ of its value based on bit position.

E.g. bit 0 has value 2^0==1, bit 1 has value 2^1==2, etc. In a UINT32, bit 31, the leftmost, would have value 2^31, but in an INT32 (2s complement), it has value -(2^31). To get the value of the number, add the weights of all its bits together.

E.g. start with a 1 followed by all 0s (100...0). Value is -(2^31) per the discussion above.

Now add 1, so that bits 31 and bit 0 are set, 100...01. This is -(2^31)+1.

Keep adding 1 till you get a result with all bits set to 1. Value is -(2^31)+2^30+2^29...+2^0==-1.

Add 1 one more time, and all the bits ripple to 0, because -1+1==0.

As you might guess, the rules for reporting overflow errors are a bit different than for straight binary.

- to find the negative of a twos-complement number, complement all the bits and add 1.

So for example, start with -1 = 111...11 as above. Complement all bits to get 000...0. Now add 1, giving 000...1, correct answer.

Now see if we can get back to -1. Complement all bits of 000...1, getting 111...0, then add 1, giving 111...1, the correct bits for -1.

Works whether the starting number is negative, positive, or 0.

Sorry, couldn't resist.

Best Regards,

Scott Bayes

Software Technical Support

Agilent Technologies, Inc.

815 14th Street S.W.

Loveland, CO, U.S.A. 80537

970 679 3799 Tel

970 635 6867 Fax

> -----Original Message-----

> From: Kathy Kiessling [mailto:k.kiessling@attbi.com]

> Sent: Wednesday, November 13, 2002 12:21 PM

> To: VEE vrf

> Subject: [vrf] Re: bits(str) function

>

>

> Here's bit more detailed explanation of the 2's complement

> representation:

>

> http://www.evergreen.edu/biophysics/tec ... s_comp.htm

>

> Kathy Kiessling

>

> ----- Original Message -----

> From: "Pickles, Warren" <Warren.Pickles@tycoelectronics.com>

> To: "VEE vrf" <vrf@it.lists.it.agilent.com>

> Sent: Wednesday, November 13, 2002 7:56 AM

> Subject: [vrf] bits(str) function

>

>

> > I've just been trying to to get to grips with the bits(str)

> function.

> > Thought I would share what I found, because it's not in the

> help file.

> >

> > According to the help, the bits(str) function "returns an

> Int32 with the

> > value of the digits in the Text str, converted to Int32 as base 2"

> >

> > So eg bits("011") returns 3. Fine.

> >

> > If you give bits(str) a string greater than 32 characters long, or

> > containing characters other than 0 or 1, it errors. Fine,

> because that

> > string definitely cannot be represented as Int32.

> >

> > But there are some strings containing 32 characters, all 0

> or 1, that

> can't

> > be represented as Int32, and then bits(str) doesn't give an

> error but

> > behaves rather strangely:

> >

> > If you give it a 0 followed by 31 1's:

> > bits("01111111111111111111111111111111") returns

> 2147483647. That's the

> > expected return value, and it is the highest possible

> Int32, ie 2^31-1

> >

> > If you "add" 1 to the input string:

> > bits("10000000000000000000000000000000") returns

> -2147483648. That's the

> > lowest possible Int32, ie -2^31. The return value ideally

> ought to be

> +2^31

> > but that is outside the range of Int32.

> >

> > "Add" 1 again to the input string:

> bits("10000000000000000000000000000001")

> > returns -2147483647. And if you keep "adding" 1 to the

> input value, it

> > looks like bits(str) adds one to the return value, until you get to

> > bits("11111111111111111111111111111111") which returns -1.

> >

> > It looks like this is what happens:

> > If the last character is 1 then add 1 to the return value.

> > If the last but one character is 1 then add 2 to the return value.

> > If the last but N character is 1 then add 2^N to the return value.

> > Except if N==31, when you subtract 2^31 instead of add.

> >

> > Regards,

> > Warren Pickles

> >

> > <<BitsStr.vee>>

> >

>

>

> --------------------------------------------------------------

> --------------

> ----

>

>

> > ---

> > You are currently subscribed to vrf as: k.kiessling@attbi.com

> > To subscribe send a blank email to

> "join-vrf@it.lists.it.agilent.com".

> > To unsubscribe send a blank email to

> "leave-vrf@it.lists.it.agilent.com".

> > To send messages to this mailing list, email

> "vrf@it.lists.it.agilent.com".

> > If you need help with the mailing list send a message to

> "owner-vrf@it.lists.it.agilent.com".

> >

>

>

> ---

> You are currently subscribed to vrf as: scott_bayes@agilent.com

> To subscribe send a blank email to "join-vrf@it.lists.it.agilent.com".

> To unsubscribe send a blank email to

> "leave-vrf@it.lists.it.agilent.com".

> To send messages to this mailing list, email

> "vrf@it.lists.it.agilent.com".

> If you need help with the mailing list send a message to

> "owner-vrf@it.lists.it.agilent.com".

>

---

You are currently subscribed to vrf as: rsb@soco.agilent.com

To subscribe send a blank email to "join-vrf@it.lists.it.agilent.com".

To unsubscribe send a blank email to "leave-vrf@it.lists.it.agilent.com".

To send messages to this mailing list, email "vrf@it.lists.it.agilent.com".

If you need help with the mailing list send a message to "owner-vrf@it.lists.it.agilent.com".