Accessing Individual bytes through built-in functions

General discussion on mikroC.
Post Reply
Author
Message
pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Accessing Individual bytes through built-in functions

#1 Post by pizon » 19 Jul 2005 11:31

Q: Is there a way to access bytes in multiple-byte variables individually?

A: Yes, there is. As an extension, mikroC (as well as other mikroElektronika compilers) offers four built-in functions for this purpose: Lo(), Hi(), Higher() and Highest(). As expected, they return single byte from the lowest to the highest one, respectively. So, instead of using the 'classical' C-way of reading the multiple-byte variables:

Code: Select all

unsigned ui1;
unsigned short usa1[50];
...
  usa1[i++] = ui1;
  usa1[i++] = ui1 >> 8;
...
, you can write:

Code: Select all

...
  usa1[i++] = Lo(ui1);
  usa1[i++] = Hi(ui1);
...
Furthermore, the abovementioned functions are L-values in mikroC, which means they can be used from the left side of the '=' operator as well. So, instead of:

Code: Select all

...
  ui1 = usa1[i] | usa1[i + 1] << 8;
...
, you can write:

Code: Select all

...
  Lo(ui1) = usa1[i++];
  Hi(ui1) = usa1[i++];
...
In short terms, these functions can be very useful and can frequently replace the asm{} blocks with minimum overhead, if any. They are meant for accessing multiple-byte variables, but can be used for arrays (first four bytes) as well.

-

Post Reply

Return to “mikroC General”