ARM Technical Support Knowledge Articles

ASSIGNING BINARY VALUES TO VARIABLES

Applies to: General Topics

Answer


Information in this article applies to:


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.

Article last edited on: 2004-06-15 15:48:45

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential