gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > u8 s8

#4155 - jenswa - Fri Mar 21, 2003 2:58 pm

what's the difference between unsigned and signed?

just curious
_________________
It seems this wasn't lost after all.

#4156 - tepples - Fri Mar 21, 2003 4:02 pm

Only signed variables can hold negative values. A signed 8-bit value can range from -128 to +127. An unsigned 8-bit value can range from 0 to 255. (EDIT: corrected. Thanks Touchstone.)

Unsigned also serves a hint to the compiler that your values will never go negative. Code that uses unsigned loop counters may run slightly faster in some cases because the compiler doesn't have to take into account the remote possibility of a negative number.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.


Last edited by tepples on Fri Mar 21, 2003 8:02 pm; edited 2 times in total

#4158 - Touchstone - Fri Mar 21, 2003 4:23 pm

tepples wrote:
An unsigned 8-bit value can range from -128 to +127. A signed 8-bit value can range from 0 to 255.


The explanation got a bit mixed up. To clarify, signed 8-bit values range from -128 to +127 and unsigned range from 0 to 255.
_________________
You can't beat our meat

#4510 - delbogun - Wed Apr 02, 2003 10:21 am

since the gba doesn't support negative numbers, isn't it better to always use unsigned values and never use signed values?

#4511 - tom - Wed Apr 02, 2003 10:24 am

delbogun wrote:
since the gba doesn't support negative numbers


uh...where did you hear that ?
=)

#4512 - Daikath - Wed Apr 02, 2003 10:24 am

It uses negative numbers perfectly :). At least that is my experience.
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#4516 - delbogun - Wed Apr 02, 2003 10:42 am

hmm... my mistake. don't know where i got that from =P.

#4629 - guilt - Sat Apr 05, 2003 9:40 am

Also, signedness effects the meaning of the bitshift operators, see the output of the following code

Code:
#include <stdio.h>

void hex_p_i (FILE *fd, int b, char *e) {
  unsigned char *c = (char *) &
  fprintf (fd, "0x%0.2x%0.2x%0.2x%0.2x%s",
      (int) c[3], (int) c[2],
      (int) c[1], (int) c[0],
      e);
}

int main (int argc, char *argv[]) {
  unsigned int u = 0x80000001;
    signed int s = 0x80000001; 
 
  fprintf (stderr,
      "0x80000001, unsigned: %u, signed: %d\n",
      u, s);
 
  hex_p_i (stderr, u>>8, " <- unsigned >>8\n");
  hex_p_i (stderr, u<<8, " <- unsigned <<8\n");
  hex_p_i (stderr, s>>8, " <- signed >>8\n");
  hex_p_i (stderr, s<<8, " <- signed <<8\n");

  return 0;
}


which is
Code:

0x80000001, unsigned: 2147483649, signed: -2147483647
0x00800000 <- unsigned >>8
0x00000100 <- unsigned <<8
0xff800000 <- signed >>8
0x00000100 <- signed <<8


When doing bit level manipulation keeping you signedness straight is *very* important.

guilt