lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Oct 23, 2014 at 03:37:32PM -0700, Coda Highland wrote:
> On Thu, Oct 23, 2014 at 3:17 PM, William Ahern
> <william@25thandclement.com> wrote:
> >> That is, 2^7 +1 is -128 for a "signed char" (or whatever) or it's 129 for
> >> an unsigned char, even though the rvalue's bits are the same...?
> >
> > I think you may have meant -127. But the bits are not necessarily the same.
> > That's only the case in two's complement representation. You have to be
> > careful about differentiating value and representation.[1]
> 
> No, he meant -128, the range of a signed byte in two's complement is
> -128 to +127.
> 

I think you're missing the +1. 2^7 is 1000000 which is -128 in two's
complement. But 2^7+1 is 10000001 which is -127.

Try running:

#include <stdio.h>

int main(void) {
	int i = (1<<7) + 1;
        printf("signed:%d unsigned:%d\n", (char)i, (unsigned char)i);
        return 0;
}

I get

$ foo
signed:-127 unsigned:129