using hex number to count in the For loop

Thread Starter

gary1wang

Joined Sep 18, 2008
23
Hi Guys

I am trying to to wrtie a value in a 32 bit register using a for loop as shown example below.

do i define the counter as

int counter=1;

or int counter= 0x00000001

or there is another way to define a variable in hex;




#include <stdio.h>
#include <LPC23xx.H>
#define A 0xffffffff
int main(void)
{
int counter;
int counter1=A;
for( counter=0; counter<A; counter++)
{
register=counter;


}

for( counter1=A; counter>0; counter--)
{

register=counter;


}
}
 

MrChips

Joined Oct 2, 2009
30,810
This is a common confusion with newcomers.

Numbers on a computer are always stored in binary.
It does not matter how you write it. I will be converted into binary by the compiler.
Hence,

A = 12;
A = 0b1100;
A = 0x0C;

all give the same result.

If you are creating a 32-bit unsigned integer, you should use the correct data type, such as

uint32_t counter;

If this is not recognized by your compiler, then you should create it:

typedef unsigned long int uint32_t;


There are only 10 types of people in the world. Those who understand binary and those who don't.
 
Last edited:

vpoko

Joined Jan 5, 2012
267
As MrChips said, the base that you choose to represent your number in is unimportant. Every integer has a cardinality that doesn't change regardless of the base (which makes sense, if you get a dozen donuts, you still have the same number of donuts whether you choose to write it as "12 donuts", "0xC donuts" or "b1100 donuts"). You're welcome to use any base supported by the compiler, including in a for loop, but you don't gain any extra functionality from doing it.
 
Top