Keil Logo

GENERAL: ASSIGNING BINARY VALUES TO VARIABLES


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.03
  • C251 Version 2.14
  • C51 Version 5.50

QUESTION

I wish to assign a value to a variable, but specify the value in binary. How can I do it?

ANSWER

You can write a macro. Look at the following code which defines a macro and uses it:

#define BIN_TO_BYTE(b7,b6,b5,b4,b3,b2,b1,b0) ((b7 << 7)+(b6 << 6)+(b5 << 5)+(b4 << 4)+(b3 << 3)+(b2 << 2)+(b1 << 1)+b0)

void main(void)
{
  volatile unsigned char x;

  x = BIN_TO_BYTE(1,1,1,1,1,0,0,0);
}

The code generated for the assignment is simply:

MOV     x,#0F8H

The compiler evaluates the constant value at compile-time and inserts it into the assembler as a constant. Note that the value will be evaluated at run-time if variables are used in place of 1s or 0s. This may generate a lot of code.

A similar method may be used for unsigned ints and longs.

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Tuesday, June 15, 2004


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.