Frequently Asked Questions

How does the compiler handle converting between bytes and words?

In an assignment such as:

bytevar = wordvar;

The most significant BYTE is lost. This is the same result as:

bytevar = wordvar & 0xff;

The following will yield just the most significant BYTE: For Example:

bytevar = wordvar >> 8;

Any arithmetic or relational expression involving both bytes and words will perform word operations, and treat the bytes as words with the top byte 0. For example:

wordvar= 0x1234; 
bytevar= 0x34; 
if(wordvar==bytevar) //will be FALSE

Any arithmetic operations that only involve bytes will yield a byte result even when assigned to word.

bytevar1 = 0x80;
bytevar2 = 0x04;
wordvar = bytevar1 * bytevar2; //wordvar will be 0

However, typecasting may be used to force word arithmetic:

wordvar = (long) bytevar1 * (long) bytevar2; //wordvar will be 0x200