# Determining sign of decmial number

Started by February 8, 2010
I'm writing a program that will swap the most significant 4-bits with the least significant 4 bits of only the negative bytes of a block of 8 bytes. The numbers in the block are a mix of decimal and hex numbers. The only problem I'm having is determining whether a positive decimal number is positive. As an example the decimal number 220 is 11011100. If I test bit 7 it will show 220 as a negative number, when in fact it is positive. How would I test a decimal number for being positive or for being negative if that is easier?
Thanks

Positive or negative numbers are only a human convention.
I mean with 8 bits, in C, you can have
"unsigned char", numbers between 0 and 255 (0000 0000 and 1111 1111)
And "char" (so called "signed char"), numbers between -128 to +127

So, with this human convention, signed char are computed in "2's
complement": for example, to compute "-1",
1) you take the absolute value (1) : 0000 0001
2) you complement bit/bit : 1111 1110
1111 1110
+ 0000 0001
= 1111 1111
So, "-1" is coded in a byte by 1111 1111

You can check than (-1) + (+1) give you 0 :
1111 1111 (code for -1)
+ 0000 0001 (code for +1)
= 0000 0000 (code for 0)

Other example : -127. +127 is coded 0111 1111
So 1000 0000 (1's complement of +127)
+ 0000 0001
= 1000 0001 (code for -127)

You can see (-127) - (+1) equal -128
1000 0001 (code for -127)
- 0000 0001 (code for +1)
= 1000 0000 (code for -128)

The carry is not used. Because you tell system to compute in 8 bits (or 16,
or 32, or 64 bits...). When you are a human, you automatically use the
carry, and add a number. The machine don't.

That means,
- if you use unsigned char : 255 + 1 equal 0 (like a miles counter for a
car; your car can't create more digits it have)
- if you use signed char : 127 + 1 equal -128
- if you use unsigned int (for example with 16 bits) : 65535 + 1 equal 0
- if you use signed int (idem, 16 bits) : 32767 + 1 equal -32768

So, as a conclusion, YOU decide what means the number in you system : signed
or not. I you decided "signed", so the most significant bit is the sign.

Hope this helps you.

Joel Petrique
-----Message d'origine-----
De: 6... [mailto:6...] De la part de
AlD
Envoy lundi 8 frier 2010 07:56
: 6...
Objet: [68HC12] Determining sign of decmial number

I'm writing a program that will swap the most significant 4-bits with the
least significant 4 bits of only the negative bytes of a block of 8 bytes.
The numbers in the block are a mix of decimal and hex numbers. The only
problem I'm having is determining whether a positive decimal number is
positive. As an example the decimal number 220 is 11011100. If I test bit 7
it will show 220 as a negative number, when in fact it is positive. How
would I test a decimal number for being positive or for being negative if
that is easier?
Thanks

If 11011100 is 220 decimal, then there are no 8-bit bytes that can represent a negative number; a byte can represent the decimal range from 0 to 255 or from -128 to 127 but not both.

Emmett Redd Ph.D. mailto:E...@missouristate.edu
Professor (417)836-5221
Department of Physics, Astronomy, and Materials Science
Missouri State University Fax (417)836-6226
901 SOUTH NATIONAL Lab (417)836-3770
SPRINGFIELD, MO 65897 USA Dept (417)836-5131

"In theory there is no difference between theory and practice. In practice there is." -- Yogi Berra or Jan van de Snepscheut

________________________________________
From: 6... [6...] On Behalf Of AlD [a...@cox.net]
Sent: Monday, February 08, 2010 12:55 AM
To: 6...
Subject: [68HC12] Determining sign of decmial number

I'm writing a program that will swap the most significant 4-bits with the least significant 4 bits of only the negative bytes of a block of 8 bytes. The numbers in the block are a mix of decimal and hex numbers. The only problem I'm having is determining whether a positive decimal number is positive. As an example the decimal number 220 is 11011100. If I test bit 7 it will show 220 as a negative number, when in fact it is positive. How would I test a decimal number for being positive or for being negative if that is easier?
Thanks

Right, but how would I determine whether the number is decimal or hex number. If it were a decimal I could assume it was a positive number. I guess the decimal number could be decimal -27, which could be represented by 8 bit number? Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2. Thanks for the help.

--- In 6..., "Redd, Emmett R" wrote:
>
> If 11011100 is 220 decimal, then there are no 8-bit bytes that can represent a negative number; a byte can represent the decimal range from 0 to 255 or from -128 to 127 but not both.
>
> Emmett Redd Ph.D. mailto:EmmettRedd@...
> Professor (417)836-5221
> Department of Physics, Astronomy, and Materials Science
> Missouri State University Fax (417)836-6226
> 901 SOUTH NATIONAL Lab (417)836-3770
> SPRINGFIELD, MO 65897 USA Dept (417)836-5131
>
> "In theory there is no difference between theory and practice. In practice there is." -- Yogi Berra or Jan van de Snepscheut
>
> ________________________________________
> From: 6... [6...] On Behalf Of AlD [al_delgado@...]
> Sent: Monday, February 08, 2010 12:55 AM
> To: 6...
> Subject: [68HC12] Determining sign of decmial number
>
> I'm writing a program that will swap the most significant 4-bits with the least significant 4 bits of only the negative bytes of a block of 8 bytes. The numbers in the block are a mix of decimal and hex numbers. The only problem I'm having is determining whether a positive decimal number is positive. As an example the decimal number 220 is 11011100. If I test bit 7 it will show 220 as a negative number, when in fact it is positive. How would I test a decimal number for being positive or for being negative if that is easier?
> Thanks
>
>
>

Your "number sequence" is not really a number sequence but a sequence of
8-bit codes.

These codes can be interpretated in many ways depending on the context.

For example the code 63 (= \$3f as hex = 00111111 as binary) can be
interpretated as:

- the number 63 (if the codes represent unsigned numbers)
- the number -193 (if the codes represent signed numbers)
- the character '?' (if the codes represent ascii characters)
- six lamps out of eight turned on (if the code is used to control lamps)

In this case (based on the given task to solve :) the codes should probably
be interpreted as signed numbers and it seems that someone is just trying to
fool someone by putting in the *code* 220 (= \$dc = 11011100) which
represents the number -36 when interpreted as signed number.
> -----Original Message-----
> From: 6... [mailto:6...]
> On Behalf Of AlD
> Sent: Monday, February 08, 2010 6:37 PM
> To: 6...
> Subject: [68HC12] Re: Determining sign of decmial number
> Right, but how would I determine whether the number is
> decimal or hex number. If it were a decimal I could assume it
> was a positive number. I guess the decimal number could be
> decimal -27, which could be represented by 8 bit number?
> Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2.
> Thanks for the help.
>
> --- In 6..., "Redd, Emmett R"
> wrote:
> >
> > If 11011100 is 220 decimal, then there are no 8-bit bytes
> that can represent a negative number; a byte can represent
> the decimal range from 0 to 255 or from -128 to 127 but not both.
> >
> > Emmett Redd Ph.D. mailto:EmmettRedd@...
> > Professor (417)836-5221
> > Department of Physics, Astronomy, and Materials Science
> > Missouri State University Fax (417)836-6226
> > 901 SOUTH NATIONAL Lab (417)836-3770
> > SPRINGFIELD, MO 65897 USA Dept (417)836-5131
> >
> > "In theory there is no difference between theory and
> practice. In practice there is." -- Yogi Berra or Jan van de
> Snepscheut
> >
> > ________________________________________
> > From: 6... [6...] On
> > Sent: Monday, February 08, 2010 12:55 AM
> > To: 6...
> > Subject: [68HC12] Determining sign of decmial number
> >
> > I'm writing a program that will swap the most significant
> 4-bits with the least significant 4 bits of only the negative
> bytes of a block of 8 bytes. The numbers in the block are a
> mix of decimal and hex numbers. The only problem I'm having
> is determining whether a positive decimal number is positive.
> As an example the decimal number 220 is 11011100. If I test
> bit 7 it will show 220 as a negative number, when in fact it
> is positive. How would I test a decimal number for being
> positive or for being negative if that is easier?
> > Thanks
> >
> >
> >
> >
> >
> >
> >
>
Sorry, forget about the 63 = -193, it is too late here :)

> -----Original Message-----
> From: 6... [mailto:6...]
> On Behalf Of Anders Friberg
> Sent: Monday, February 08, 2010 7:24 PM
> To: 6...
> Subject: RE: [68HC12] Re: Determining sign of decmial number
>
> Your "number sequence" is not really a number sequence but a
> sequence of
> 8-bit codes.
>
> These codes can be interpretated in many ways depending on
> the context.
>
> For example the code 63 (= \$3f as hex = 00111111 as binary) can be
> interpretated as:
>
> - the number 63 (if the codes represent unsigned numbers)
> - the number -193 (if the codes represent signed numbers)
> - the character '?' (if the codes represent ascii characters)
> - six lamps out of eight turned on (if the code is used to
> control lamps)
>
> In this case (based on the given task to solve :) the codes
> should probably
> be interpreted as signed numbers and it seems that someone is
> just trying to
> fool someone by putting in the *code* 220 (= \$dc = 11011100) which
> represents the number -36 when interpreted as signed number.
> > -----Original Message-----
> > From: 6... [mailto:6...]
> > On Behalf Of AlD
> > Sent: Monday, February 08, 2010 6:37 PM
> > To: 6...
> > Subject: [68HC12] Re: Determining sign of decmial number
> >
> >
> >
> >
> >
> >
> > Right, but how would I determine whether the number is
> > decimal or hex number. If it were a decimal I could assume it
> > was a positive number. I guess the decimal number could be
> > decimal -27, which could be represented by 8 bit number?
> > Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2.
> > Thanks for the help.
> >
> > --- In 6..., "Redd, Emmett R"
> > wrote:
> > >
> > > If 11011100 is 220 decimal, then there are no 8-bit bytes
> > that can represent a negative number; a byte can represent
> > the decimal range from 0 to 255 or from -128 to 127 but not both.
> > >
> > > Emmett Redd Ph.D. mailto:EmmettRedd@...
> > > Professor (417)836-5221
> > > Department of Physics, Astronomy, and Materials Science
> > > Missouri State University Fax (417)836-6226
> > > 901 SOUTH NATIONAL Lab (417)836-3770
> > > SPRINGFIELD, MO 65897 USA Dept (417)836-5131
> > >
> > > "In theory there is no difference between theory and
> > practice. In practice there is." -- Yogi Berra or Jan van de
> > Snepscheut
> > >
> > > ________________________________________
> > > From: 6... [6...] On
> > Behalf Of AlD [al_delgado@...]
> > > Sent: Monday, February 08, 2010 12:55 AM
> > > To: 6...
> > > Subject: [68HC12] Determining sign of decmial number
> > >
> > > I'm writing a program that will swap the most significant
> > 4-bits with the least significant 4 bits of only the negative
> > bytes of a block of 8 bytes. The numbers in the block are a
> > mix of decimal and hex numbers. The only problem I'm having
> > is determining whether a positive decimal number is positive.
> > As an example the decimal number 220 is 11011100. If I test
> > bit 7 it will show 220 as a negative number, when in fact it
> > is positive. How would I test a decimal number for being
> > positive or for being negative if that is easier?
> > > Thanks
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
> >
> >
> >
Al,

Joel gave you a long and detailed and I gave you a short one. My answer implied that you have to decide what number you want a byte to represent. Joel was more direct, "YOU decide what means the number in you[r] system".

Once you decide you need signed numbers, the most significant bit of the byte is the sign and you are limited to -128 to 127 decimal (\$80 to \$ff, \$00 to \$7f in hexadecimal). You can't have 220 decimal (\$dc hexadecimal) and have signed numbers represented in a byte.

So, what do you choose: larger unsigned numbers or smaller signed numbers?

Emmett Redd, Ph.D., Professor mailto:E...@MissouriState.Edu
Physics, Astronomy, and Materials Science Office: 417-836-5221
Missouri State University Dept: 417-838-5131
901 S NATIONAL AVENUE FAX: 417-836-6226
SPRINGFIELD, MO 65897 USA

-----Original Message-----
From: 6... [mailto:6...] On Behalf Of AlD
Sent: Monday, February 08, 2010 11:37 AM
To: 6...
Subject: [68HC12] Re: Determining sign of decmial number

Right, but how would I determine whether the number is decimal or hex number. If it were a decimal I could assume it was a positive number. I guess the decimal number could be decimal -27, which could be represented by 8 bit number? Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2. Thanks for the help.

--- In 6..., "Redd, Emmett R" wrote:
>
> If 11011100 is 220 decimal, then there are no 8-bit bytes that can represent a negative number; a byte can represent the decimal range from 0 to 255 or from -128 to 127 but not both.
>
> Emmett Redd Ph.D. mailto:EmmettRedd@...
> Professor (417)836-5221
> Department of Physics, Astronomy, and Materials Science
> Missouri State University Fax (417)836-6226
> 901 SOUTH NATIONAL Lab (417)836-3770
> SPRINGFIELD, MO 65897 USA Dept (417)836-5131
>
> "In theory there is no difference between theory and practice. In practice there is." -- Yogi Berra or Jan van de Snepscheut
>
> ________________________________________
> From: 6... [6...] On Behalf Of AlD [al_delgado@...]
> Sent: Monday, February 08, 2010 12:55 AM
> To: 6...
> Subject: [68HC12] Determining sign of decmial number
>
> I'm writing a program that will swap the most significant 4-bits with the least significant 4 bits of only the negative bytes of a block of 8 bytes. The numbers in the block are a mix of decimal and hex numbers. The only problem I'm having is determining whether a positive decimal number is positive. As an example the decimal number 220 is 11011100. If I test bit 7 it will show 220 as a negative number, when in fact it is positive. How would I test a decimal number for being positive or for being negative if that is easier?
> Thanks
>
>
>

You mean the *teacher* decides? :)

> -----Original Message-----
> From: 6... [mailto:6...]
> On Behalf Of Redd, Emmett R
> Sent: Monday, February 08, 2010 7:29 PM
> To: 6...
> Subject: RE: [68HC12] Re: Determining sign of decmial number
>
> Al,
>
> Joel gave you a long and detailed and I gave you a short one.
> My answer implied that you have to decide what number you
> want a byte to represent. Joel was more direct, "YOU decide
> what means the number in you[r] system".
>
> Once you decide you need signed numbers, the most significant
> bit of the byte is the sign and you are limited to -128 to
> 127 decimal (\$80 to \$ff, \$00 to \$7f in hexadecimal). You
> can't have 220 decimal (\$dc hexadecimal) and have signed
> numbers represented in a byte.
>
> So, what do you choose: larger unsigned numbers or smaller
> signed numbers?
>
> Emmett Redd, Ph.D., Professor mailto:E...@MissouriState.Edu
> Physics, Astronomy, and Materials Science Office: 417-836-5221
> Missouri State University Dept: 417-838-5131
> 901 S NATIONAL AVENUE FAX: 417-836-6226
> SPRINGFIELD, MO 65897 USA
>
> -----Original Message-----
> From: 6... [mailto:6...]
> On Behalf Of AlD
> Sent: Monday, February 08, 2010 11:37 AM
> To: 6...
> Subject: [68HC12] Re: Determining sign of decmial number
> Right, but how would I determine whether the number is
> decimal or hex number. If it were a decimal I could assume it
> was a positive number. I guess the decimal number could be
> decimal -27, which could be represented by 8 bit number?
> Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2.
> Thanks for the help.
>
> --- In 6..., "Redd, Emmett R"
> wrote:
> >
> > If 11011100 is 220 decimal, then there are no 8-bit bytes
> that can represent a negative number; a byte can represent
> the decimal range from 0 to 255 or from -128 to 127 but not both.
> >
> > Emmett Redd Ph.D. mailto:EmmettRedd@...
> > Professor (417)836-5221
> > Department of Physics, Astronomy, and Materials Science
> > Missouri State University Fax (417)836-6226
> > 901 SOUTH NATIONAL Lab (417)836-3770
> > SPRINGFIELD, MO 65897 USA Dept (417)836-5131
> >
> > "In theory there is no difference between theory and
> practice. In practice there is." -- Yogi Berra or Jan van de
> Snepscheut
> >
> > ________________________________________
> > From: 6... [6...] On
> > Sent: Monday, February 08, 2010 12:55 AM
> > To: 6...
> > Subject: [68HC12] Determining sign of decmial number
> >
> > I'm writing a program that will swap the most significant
> 4-bits with the least significant 4 bits of only the negative
> bytes of a block of 8 bytes. The numbers in the block are a
> mix of decimal and hex numbers. The only problem I'm having
> is determining whether a positive decimal number is positive.
> As an example the decimal number 220 is 11011100. If I test
> bit 7 it will show 220 as a negative number, when in fact it
> is positive. How would I test a decimal number for being
> positive or for being negative if that is easier?
> > Thanks
> >
> >
> >
> >
> >
> >
> >
>
Emmett, Anders and Joel

Thanks for the input,I understand what you are saying, but as Anders puts it so well, "someone is trying to fool someone." I have to deal with the sequence given: 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2. Is there a way to detect that 220 is a positive decimal number and bypass it, a way to detect the number base prefix? If the data is in hex I can go test it for negative or positive and do the swap?

--- In 6..., "Redd, Emmett R" wrote:
>
> Al,
>
> Joel gave you a long and detailed and I gave you a short one. My answer implied that you have to decide what number you want a byte to represent. Joel was more direct, "YOU decide what means the number in you[r] system".
>
> Once you decide you need signed numbers, the most significant bit of the byte is the sign and you are limited to -128 to 127 decimal (\$80 to \$ff, \$00 to \$7f in hexadecimal). You can't have 220 decimal (\$dc hexadecimal) and have signed numbers represented in a byte.
>
> So, what do you choose: larger unsigned numbers or smaller signed numbers?
>
> Emmett Redd, Ph.D., Professor mailto:EmmettRedd@...
> Physics, Astronomy, and Materials Science Office: 417-836-5221
> Missouri State University Dept: 417-838-5131
> 901 S NATIONAL AVENUE FAX: 417-836-6226
> SPRINGFIELD, MO 65897 USA
>
> -----Original Message-----
> From: 6... [mailto:6...] On Behalf Of AlD
> Sent: Monday, February 08, 2010 11:37 AM
> To: 6...
> Subject: [68HC12] Re: Determining sign of decmial number
> Right, but how would I determine whether the number is decimal or hex number. If it were a decimal I could assume it was a positive number. I guess the decimal number could be decimal -27, which could be represented by 8 bit number? Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2. Thanks for the help.
>
> --- In 6..., "Redd, Emmett R" wrote:
> >
> > If 11011100 is 220 decimal, then there are no 8-bit bytes that can represent a negative number; a byte can represent the decimal range from 0 to 255 or from -128 to 127 but not both.
> >
> > Emmett Redd Ph.D. mailto:EmmettRedd@
> > Professor (417)836-5221
> > Department of Physics, Astronomy, and Materials Science
> > Missouri State University Fax (417)836-6226
> > 901 SOUTH NATIONAL Lab (417)836-3770
> > SPRINGFIELD, MO 65897 USA Dept (417)836-5131
> >
> > "In theory there is no difference between theory and practice. In practice there is." -- Yogi Berra or Jan van de Snepscheut
> >
> > ________________________________________
> > From: 6... [6...] On Behalf Of AlD [al_delgado@]
> > Sent: Monday, February 08, 2010 12:55 AM
> > To: 6...
> > Subject: [68HC12] Determining sign of decmial number
> >
> > I'm writing a program that will swap the most significant 4-bits with the least significant 4 bits of only the negative bytes of a block of 8 bytes. The numbers in the block are a mix of decimal and hex numbers. The only problem I'm having is determining whether a positive decimal number is positive. As an example the decimal number 220 is 11011100. If I test bit 7 it will show 220 as a negative number, when in fact it is positive. How would I test a decimal number for being positive or for being negative if that is easier?
> > Thanks
> >
> >
> >
> >
> >
> >
> >
>
yep

--- In 6..., "Anders Friberg" wrote:
>
> You mean the *teacher* decides? :)
>
> > -----Original Message-----
> > From: 6... [mailto:6...]
> > On Behalf Of Redd, Emmett R
> > Sent: Monday, February 08, 2010 7:29 PM
> > To: 6...
> > Subject: RE: [68HC12] Re: Determining sign of decmial number
> >
> > Al,
> >
> > Joel gave you a long and detailed and I gave you a short one.
> > My answer implied that you have to decide what number you
> > want a byte to represent. Joel was more direct, "YOU decide
> > what means the number in you[r] system".
> >
> > Once you decide you need signed numbers, the most significant
> > bit of the byte is the sign and you are limited to -128 to
> > 127 decimal (\$80 to \$ff, \$00 to \$7f in hexadecimal). You
> > can't have 220 decimal (\$dc hexadecimal) and have signed
> > numbers represented in a byte.
> >
> > So, what do you choose: larger unsigned numbers or smaller
> > signed numbers?
> >
> > Emmett Redd, Ph.D., Professor mailto:EmmettRedd@...
> > Physics, Astronomy, and Materials Science Office: 417-836-5221
> > Missouri State University Dept: 417-838-5131
> > 901 S NATIONAL AVENUE FAX: 417-836-6226
> > SPRINGFIELD, MO 65897 USA
> >
> >
> >
> > -----Original Message-----
> > From: 6... [mailto:6...]
> > On Behalf Of AlD
> > Sent: Monday, February 08, 2010 11:37 AM
> > To: 6...
> > Subject: [68HC12] Re: Determining sign of decmial number
> >
> >
> >
> >
> >
> >
> > Right, but how would I determine whether the number is
> > decimal or hex number. If it were a decimal I could assume it
> > was a positive number. I guess the decimal number could be
> > decimal -27, which could be represented by 8 bit number?
> > Example number sequence, 220,\$a1,\$3c,\$da,\$b5,27,\$e4,\$f2.
> > Thanks for the help.
> >
> > --- In 6..., "Redd, Emmett R"
> > wrote:
> > >
> > > If 11011100 is 220 decimal, then there are no 8-bit bytes
> > that can represent a negative number; a byte can represent
> > the decimal range from 0 to 255 or from -128 to 127 but not both.
> > >
> > > Emmett Redd Ph.D. mailto:EmmettRedd@
> > > Professor (417)836-5221
> > > Department of Physics, Astronomy, and Materials Science
> > > Missouri State University Fax (417)836-6226
> > > 901 SOUTH NATIONAL Lab (417)836-3770
> > > SPRINGFIELD, MO 65897 USA Dept (417)836-5131
> > >
> > > "In theory there is no difference between theory and
> > practice. In practice there is." -- Yogi Berra or Jan van de
> > Snepscheut
> > >
> > > ________________________________________
> > > From: 6... [6...] On
> > Behalf Of AlD [al_delgado@]
> > > Sent: Monday, February 08, 2010 12:55 AM
> > > To: 6...
> > > Subject: [68HC12] Determining sign of decmial number
> > >
> > > I'm writing a program that will swap the most significant
> > 4-bits with the least significant 4 bits of only the negative
> > bytes of a block of 8 bytes. The numbers in the block are a
> > mix of decimal and hex numbers. The only problem I'm having
> > is determining whether a positive decimal number is positive.
> > As an example the decimal number 220 is 11011100. If I test
> > bit 7 it will show 220 as a negative number, when in fact it
> > is positive. How would I test a decimal number for being
> > positive or for being negative if that is easier?
> > > Thanks
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
> >
> >
> >