Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"char a = 0x80;" results in "a == 0xffffff80"
20-08-2012, 04:36 PM, (This post was last modified: 20-08-2012, 04:40 PM by raphman.)
#1
"char a = 0x80;" results in "a == 0xffffff80"
Hi. I'm relatively new to Pinguino and absolutely new to this forum. Please forgive me if I'm using the wrong medium for this bug report / question.

This code, compiled for the Olimex Pinguino32 board,
Code:
void setup()
{                
}

void loop()
{
  char a = 0x7f;
  char b = 0x80;
  CDC.printf("a: %x - b: %x\r\n", a, b);
}

outputs this:

Code:
a: 7f - b: ffffff80

As long as I assign numbers from 0x00 to 0x7f to char variables, everything is ok. If I assign larger values, i.e. 0x80 and up, to the variables, they apparently get promoted to ints and the upper bits are filled with '1's.

This is not a bug in the printf() function, as the incorrect value is also used in all arithmetic operations (which caused me a lot of "wtf?"s).

Can anybody help me finding out what the underlying cause for this bug is, and how to fix it?

clarification: the following code works as expected. However, I would like to use chars, not ints.
And the behavior I posted is definitely a bug.

Code:
void setup()
{                
}

void loop()
{
   int a = 0x7f;
   int b = 0x80;
   CDC.printf("a: %x - b: %x\r\n", a, b);
}
Reply
20-08-2012, 04:56 PM,
#2
RE: "char a = 0x80;" results in "a == 0xffffff80"
Hi Raphman

char 0x80 is actually character -0x7f.

You need to change
char a = 0x7f;
char b = 0x80;
to
unsigned char a = 0x7f;
unsigned char b = 0x80;
to get the desired result.

(20-08-2012, 04:36 PM)raphman Wrote: Hi. I'm relatively new to Pinguino and absolutely new to this forum. Please forgive me if I'm using the wrong medium for this bug report / question.

This code, compiled for the Olimex Pinguino32 board,
Code:
void setup()
{                
}

void loop()
{
  char a = 0x7f;
  char b = 0x80;
  CDC.printf("a: %x - b: %x\r\n", a, b);
}

outputs this:

Code:
a: 7f - b: ffffff80

As long as I assign numbers from 0x00 to 0x7f to char variables, everything is ok. If I assign larger values, i.e. 0x80 and up, to the variables, they apparently get promoted to ints and the upper bits are filled with '1's.

This is not a bug in the printf() function, as the incorrect value is also used in all arithmetic operations (which caused me a lot of "wtf?"s).

Can anybody help me finding out what the underlying cause for this bug is, and how to fix it?

clarification: the following code works as expected. However, I would like to use chars, not ints.
And the behavior I posted is definitely a bug.

Code:
void setup()
{                
}

void loop()
{
   int a = 0x7f;
   int b = 0x80;
   CDC.printf("a: %x - b: %x\r\n", a, b);
}
Boards:
Arduino uno full, 2009 barebones
* Arduino version: 1.0.3 beta
* Processing version: 2.-0a4
PIC32-Pinguino MX440F rev. c
* Pinguino version: svn latest x.3
STM32F-Discovery
Arduino shields:
W5100 Ethernet and SD
OS: ubuntu 12.10 64 bit
Reply
20-08-2012, 06:04 PM,
#3
RE: "char a = 0x80;" results in "a == 0xffffff80"
(20-08-2012, 04:56 PM)gregoryfenton Wrote: char 0x80 is actually character -0x7f.

You need to change
char a = 0x7f;
char b = 0x80;
to
unsigned char a = 0x7f;
unsigned char b = 0x80;
to get the desired result.

Thank you! It is so obvious now. I was absolutely sure that assignment of 0x80 to a char would only set the bits, not involve any integer promotion.
Reply
20-08-2012, 06:39 PM,
#4
RE: "char a = 0x80;" results in "a == 0xffffff80"
Glad I could help Smile
Happy hacking!
(20-08-2012, 06:04 PM)raphman Wrote:
(20-08-2012, 04:56 PM)gregoryfenton Wrote: char 0x80 is actually character -0x7f.

You need to change
char a = 0x7f;
char b = 0x80;
to
unsigned char a = 0x7f;
unsigned char b = 0x80;
to get the desired result.

Thank you! It is so obvious now. I was absolutely sure that assignment of 0x80 to a char would only set the bits, not involve any integer promotion.
Boards:
Arduino uno full, 2009 barebones
* Arduino version: 1.0.3 beta
* Processing version: 2.-0a4
PIC32-Pinguino MX440F rev. c
* Pinguino version: svn latest x.3
STM32F-Discovery
Arduino shields:
W5100 Ethernet and SD
OS: ubuntu 12.10 64 bit
Reply
21-08-2012, 07:31 PM,
#5
RE: "char a = 0x80;" results in "a == 0xffffff80"
Strictly, the problem is that when you have char b and then use it in the printf it gets widened (in this case sign-extended) because any char will be passed as a value of the size of an int.

In C, a char may or may not be sign-extended (compiler decides; this one does it by default), so you have to code to make it work either way. An unsigned char is a good fix!

John
Reply
22-08-2012, 01:18 PM,
#6
RE: "char a = 0x80;" results in "a == 0xffffff80"
(21-08-2012, 07:31 PM)pingotg Wrote: Strictly, the problem is that when you have char b and then use it in the printf it gets widened

Thanks, you are right. In my specific case, integer promotion already happened somewhere else in the code which produced extremely strange behavior. That's what I meant to write instead of what I actually wrote Smile
Reply
22-08-2012, 02:50 PM,
#7
RE: "char a = 0x80;" results in "a == 0xffffff80"
No worries!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)