2001.06.12 09:42 "Possible bug with SBYTE type", by Neil Talsania

2001.07.27 20:24 "Re: Possible bug with SBYTE type", by Andreas R. Kleinert

Here is what I am trying to do. I have an image that is of type CHAR, thus the range of values are from -128 to +127. I am trying to set the SMIN tag to -128, however when I get the value back it is +128.

So, then I started looking at the code. And I verified with a debugger that the value that I am trying to write is -128. The problem seems to be when I read it back in. Because the type for the SMIN tag is of type any, and my data is CHAR, it is getting written out as a SBYTE type. When this gets read back in, the data is promoted to a USHORT in the call to FetchByteArray, then the value is casted back to a SHORT, however by this time the sign has been lost, and now my -128 (which I believe would be 10000000 in twos complement binary representation for a single byte) has become +128 (which for a short would be represented by 0000000010000000).

For the following code my compiler prints "65408" and "-128":


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
   signed char  sB;
 unsigned short uW;
   signed short sW;

   sB = -128;
   uW = sB;   printf("unsigned short: %lu\n", uW);
   sW = uW;   printf("signed short:   %ld\n", sW);



If I replace the "sB" declaration by

   unsigned char sB;

it prints "128" and "128".

Conclusion: your C compiler's default setting for "CHAR" (char ?) is not "signed" but "unsigned". This is ok for the C standard, but you may wish to reconfigure your compiler and/or CHAR typedef.

Andreas_Kleinert@t-online.de  | http://www.ar-kleinert.de             |
Freelance Consultant & Writer | Software Engineering                  |
 *** PerSuaSiVe SoftWorX ***  | x86 Win/Linux, 68k/PPC Amiga and more |