This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

use of bit as array index fails? (if bit is set)

Hi,

I suspect I have hit upon a bug in C51 V6.
When a bit variable is used as an index into
array of int or long int, it fails. Actually,
it fails only if the bit is 1; otherwise it
works ok. I am not 100% sure whether this
is a bug or am I doing something wrong?
For now, I replaced the bit variable by an
unsigned char, but I can not afford to do
so in the final version of my program. Is
there a workaround?

I wrote the test code below when I suspected
this bug. I tested it with the simulator and
I see un-expected results.

One User

#include <reg52.h>
#include <stdio.h>

xdata unsigned int UnIntArr[2] = {	0x1234, 0x5678	} ;
xdata unsigned long int UnLongIntArr[2] = {	0x12345678, 0x9abcdef0	} ;

void main ()
{
	unsigned char uch ;
	bit b ;

	RCAP2H = 0xff ;
	RCAP2L = 0xdc ;
	T2CON = 0x34 ;
	SCON = 0x50 ;
	TI = 1 ;

	uch = 0 ;
	b = 0 ;
	printf("first uch = 0 and b = 0\n") ;
	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[b]) ;
	// expected values = 0x1234, 0x1234 (works ok)
	printf("UnLongIntArr[uch] = %08lX, UnLongIntArr[b] = %08lX\n",UnLongIntArr[uch],UnLongIntArr[b]) ;
	// expected values = 0x12345678, 0x12345678 (works ok)

	uch = 1 ;
	b = 1 ;
	printf("now uch = 1 and b = 1\n") ;
	printf("UnIntArr[uch] = %04X, UnIntArr[b] = %04X\n",UnIntArr[uch],UnIntArr[b]) ;
	// expected values = 0x5678, 0x5678
	// BUG HERE-> you get 0x5678, 0x3456
	printf("UnLongIntArr[uch] = %08lX, UnLongIntArr[b] = %08lX\n",UnLongIntArr[uch],UnLongIntArr[b]) ;
	// expected values = 0x9ABCDEF0, 0x9ABCDEF0
	// BUG HERE TOO-> you get 0x9ABCDEF0, 0x3456789A
}

Parents Reply Children
No data