# Need help understanding some code for the 8051(Syntax related)

Status
Not open for further replies.

#### reenalalu

##### New Member
Take a look at the two snippets of code below. It was written for an 8051 micro-controller.

void main (void)
{
unsigned int a,i;
a=11;
for(i=0;i=8;i++)
{
If ((a>i)&0x1==1)
{
delay();
}

}
}

My question with regards to the above section of code are:
1.What does the IF condition in this code do? I don’t understand it. According to the person who wrote this code, the condition compares to see if each bit of a = 1/0. If it is a 1, then the delay function is called. How does this comparison work??????
2.Can this exact same if condition be used as it is on the PIC uController without any syntax change?

Here is the second piece of code again written for a 8051 uController

void main (void)
{
unsigned int a=0,i;
for(i=0;i=4;i++)
{
if (P1_1=0) //if a port is low
{
a=a|1<<i;
}
}

}

1.What does a=a|1<<i do? And how does it function?
2.Lets say when i=0,1,2,3,4 , P1_1=0. What would the value of a be in this case?
3.If P1_1=0 when i=0,1,2 and 4 but at i=3 this condition is false. What is the value of a in this case?
4.Can this line of code be used just as it is for the PIC uController (i mean with regards to syntax)?

Really looking forward to hearing from everyone

reenalalu

#### reenalalu

##### New Member
Help.please #### srikanthsamaga

##### New Member
a=11;
for(i=0;i=8;i++)
{
If ((a>i)&0x1==1)

The above if statement does nothing. The condition inside paranthesis is always true.

What does a=a|1<<i do? And how does it function

ex: if i=2, and if a=0 then the result of above exp would be
a = 0000 0100.
1<<x means that shift a 1 x times left..

#### Pommie

##### Well-Known Member
I think when you typed in the code you made a few mistakes. My guess would be that it should be,
Code:
void main (void)
{
unsigned int a,i;
a=11;
for(i=0;i[COLOR="red"]<[/COLOR]8;i++)
{
If ((a[COLOR="Red"]>[/COLOR]>i)&0x1==1)
{
delay();
}
}
}
The >> operator shifts the value right by the number after the >>. So a>>5 would shift a right 5. It then ANDs with 1 to keep the bottom bit. As a=11 =0b00001011 then delay will get called 3 times.

The above code would work the same on a pic.

The second piece of code has similar errors but the << (shift left) operator is correct.
a=a|1<<i;
Will take 1 and shift it left i times and then OR it with the previous value of a. So if i=3 then it will set bit 3 of a.

Mike.

Status
Not open for further replies.

Replies
3
Views
2K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
1
Views
4K
Replies
13
Views
14K 