12,634,651 members (24,967 online)
Rate this:
See more:
hello, i saw a demo code which change the function of trigger output from byte by byte to bit by bit, by using both of this symbol,
|= and &=
i wonder what is the function of it.. cause i cant find any solution from google~
Posted 26-Sep-12 21:53pm

Rate this:

## Solution 1

Hi,

to set a bit x, use this functionality:
number |= 1 << x;

to clear a bit x, use this code:
number &= ~(1 << x);

to toggle a bit x, use this code:
number ^= 1 << x;

See here for an explanation:
http://www.cprogramming.com/tutorial/bitwise_operators.html[^]
arnoldxx 27-Sep-12 3:02am

thanks.. simple and nice explanation~ :)
arnoldxx 27-Sep-12 3:20am

sorry i m a bit confuse with this code

if(((CStatic*)pWnd)->GetBitmap() != m_bmp_Output_ON)
{
((CStatic*)pWnd)->SetBitmap(m_bmp_Output_ON);
wResult_4th_8Bits |= 0x80;
}
else
{
((CStatic*)pWnd)->SetBitmap(m_bmpOFF);
wResult_4th_8Bits &= 0x7f;
}

can u please explain it to me? thanks
JF2015 27-Sep-12 3:34am

The line wResult_4th_8Bits |= 0x80; basically only sets the 8th bit and the line wResult_4th_8Bits &= 0x7f; resets any bit above the 7th bit and leaves the lowest 7 bit unchanged, maybe just to convert a 32bit/16bit/8bit value to 7bit. Why this code performs this action, I can't tell you - this is not clear out of the context.
arnoldxx 27-Sep-12 3:38am

haha.. its okey, ^^ thanks for the explanation

Top Experts
Last 24hrsThis month
 OriginalGriff 200 Richard MacCutchan 95 John Simmons / outlaw programmer 65 CPallini 65 ridoy 50
 OriginalGriff 2,374 ppolymorphe 1,271 Peter Leow 1,029 John Simmons / outlaw programmer 770 CPallini 686