EEVblog Electronics Community Forum

General => General Chat => Topic started by: iamwhoiam on June 03, 2012, 04:07:58 pm

Title: Hex confusion: what value is attained when I decrement 0x00?
Post by: iamwhoiam on June 03, 2012, 04:07:58 pm
Hi guys. Being used to decimal, naturally, I am confused by this; the part highlighted in red:

LED_PAUSE
MOVLW 0xFF
MOVWF LED_LOOP2

LED_PAUSE2
CLRF LED_LOOP
DECFSZ LED_LOOP,F
GOTO \$-1
DECFSZ LED_LOOP2,F
GOTO LED_PAUSE2

END

Seeing as "LED_LOOP" has been issued CLRF, and hence contains 0x00, what value does decrementing 0x00 result in; 0xFF?

Hex n00b!

Thanks.
Title: Re: Hex confusion: what value is attained when I decrement 0x00?
Post by: MikeK on June 03, 2012, 04:14:49 pm
The opposite of what happens when you increment 0xFF.
Title: Re: Hex confusion: what value is attained when I decrement 0x00?
Post by: AntiProtonBoy on June 03, 2012, 04:42:06 pm
Google says 0x00 -1 is -0x1...  :-\ or is that just Google fail?
Not necessarily a fail, it depends how the byte is interpreted.

If you interpret 11111111 as an unsigned binary, then its hex equivalent is 0xFF, or 255 decimal.
If you interpret 11111111 as a two's complement signed binary, then hex is -0x01, or -1 decimal.

That being said, hex constant values are generally interpreted as a raw collection of bits with the sign absent, thus I'd find the -0x01 notation unusual.
Title: Re: Hex confusion: what value is attained when I decrement 0x00?
Post by: caroper on June 03, 2012, 08:26:35 pm
Thanks to binary arithmetic we have finally solved Shakespeare's famous  dilemma.

2B OR NOT 2B = -1

Cheers
Chris

Title: Re: Hex confusion: what value is attained when I decrement 0x00?
Post by: Mechatrommer on June 04, 2012, 06:44:42 am
Quote from: OP
what value is attained when I decrement 0x00?
The opposite of what happens when you increment 0xFF.
(its been a while). what value will depend on how many bits per byte/register we have and architecture of the "flipflop" (is it the right term?). deducted it to 8bit PIC8 (from your example).. my answer is 0xFF with 1 in carry (or zero) bit. incrementing  0xFF will result in 0x00 with 1 in carry/zero bit.