CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

understand "signed"

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








understand "signed"
PostPosted: Mon Jun 22, 2009 1:32 pm     Reply with quote

is there a way to understand this:

why is 0xfd = -3?

Code:
int a,b;

.................... a=3;
01D4:  MOVLW  03
01D6:  MOVWF  a

.................... b=-3;
01D8:  MOVLW  FD
01DA:  MOVWF  b
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Mon Jun 22, 2009 2:21 pm     Reply with quote

Code:

    0b01111111 = 127 decimal
    0b10000000 = -128 decimal
    0b10000001 = -127 decimal
....


Explained well enough?
Ttelmah
Guest







PostPosted: Mon Jun 22, 2009 3:05 pm     Reply with quote

There are a number of different types of 'signed' numbers, depending on the language and processor involved.

Start with a single byte. Can represent 256 possible values. It is entirely up to the language authors, and any specific hardware abilities of the chip, what these values are used to represent. So you might decide (for example), to use the values to represent -100 to +155, but just adding 100 to the value before storing it. This approach is sometimes used, where you need to hold a smaller range in one direction than the other. This form is called 'Excess-N'.

Then you have a real 'oddity', called Base-2. Here instead of the bits of the byte representing powers of 2 (2^0, 2^1 etc.), you use powers of -2. So the first bit is -2^0, then the next -2^1, then -2^2 etc.. This results in the sign of each bit reversing. I do not know of any really good reason to use this, except in certain hardware circuits....

Then you run into the more common numeric formats. For these, you 'steal' one bit from the 8bit value, and call this the 'sign' bit. If a number is +ve, you leave the bit unset. Set the bit and the remaining bits are now a -ve number. For one simple form of this, you then get:

00000000 = 0
00000001 = 1
00000010 = 2
10000001 = -1
10000010 = -2

This is the simplest 'signed' form, but has the unusual feature, that you can have -0, and effectively therefore waste one value.

The next form, 'offsets' the values used for the -ve numbers by one. So you get:

00000000 = 0
00000001 = 1
00000010 = 2
10000000 = -1
10000001 = -2

This was a format used quite commonly in the early days of computing. Never seen now in anything normal.

The next format, was a variation of the simple 'sign' format, called 'ones complement'. Here the sign bit was the same, but the remaining bits of the number were 'complemented' (inverted). The basic format was the same as the first example, but with the lower bits inverted for -ve values. So:

00000000 = 0
00000001 = 1
00000010 = 2
11111110 = -1
11111101 = -2

This has the same oddity of having two zeros, but is slightly easier to use in some hardware designs. Because of this, even commoner in the early computing days. Still sometimes used now (look for references to 'ones complement').

The final form, is today the most common. This is called 'two's complement'. With this, for a -ve value, you invert _all_ the bits (including the one treated as a 'sign'), and then add one.
So:

00000000 = 0
00000001 = 1
00000010 = 2
11111111 = -1
11111110 = -2

This gets rid of the wasted 'two zeros' from the other simple forms, and has the advantage that the numbers are in direct sequence of binary values as you count 'through' zero. Makes logic simple, meaning that basically the same addition circuitry can be used for numbers 'signed' this way, and unsigned values. One slight 'downside', is one more -ve value supported than +ve values. This is the format used by the PIC (and CCS).
So in your example, 0xFD, is '11111101'. Since the top bit is set, it is a -ve number. To find the +ve number, subtract one from this, to give '11111100'. Now invert the bits to get '00000011'. Decimal 3. So 0xFD, is -3, in two's complement notation.
So the 'reason' 0xFD, is -3, is that the PIC is using two's complement notation to represent -ve numbers.

Best Wishes
Guest








PostPosted: Tue Jun 23, 2009 9:19 am     Reply with quote

Thanks for this, it's a nice explanation. I understand it:-)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group