Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

If condition could not understood

Status
Not open for further replies.

faisaldj

Newbie level 5
Joined
Jan 29, 2008
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,335
Can somebody elaborate the If condition in this sample code
I try to debug it and everytime getting

00000000
or
00000001

but could not interpret the logic.


////////////////////////////// Code ////////////////////////

phase _buff is 8 bit.
unsigned char phase_buff=0x60;

for (b = 0; b < 9; b++)

{

if ( ( phase_buff[a] >> (7 - b) ) & 1 ) {
{
do_something;
}
}

regards,
faisal.
 

inside the if statement you shift right (7-b) bits and then check if the lsb is 1 (the other bits are masked)

Alex
 
yes, this code for (b = 0; b < 9; b++) executes from b=0 to b=8
so if phase_buff[a] is 8 bit , b should be 0 to 7.

There are also 2 ways to do the masking inside the if,
the first is to shift right the variable and mask the 7 bits using 1 as you are doing
or you can shift the 1 to the left, I'm not sure which is faster, the result will be the same, i think this is easier to read.
if ( phase_buff[a] & (1<<b) )
so you are masking with 00000001 00000010 00000100 etc

Alex
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top