From: "Christopher Nelson" To: Subject: Re: bits and flags Date: Fri, 9 Jul 1999 13:18:40 -0600 Message-ID: <01beca3f$da040700$LocalHost@thendren> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.71.1712.3 X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3 Reply-To: djgpp AT delorie DOT com -----Original Message----- From: Darren Noble To: djgpp AT delorie DOT com Date: Thursday, July 08, 1999 3:16 AM Subject: Re: bits and flags >On Thu, 08 Jul 1999, you wrote: >> Lets say I have >> char ch=1; >> >> Now I can test if a bit is "on" >> if(ch&1) >> ....... >> >> but how can I set a bit.. Lets say bit 4? >> and how can I "turn off" a bit? > >If you take 2 to the 4th power witch is 16, you can say: > > ch-=16; > >and that will turn of the 4th bit if it is on. If it is off and you want it on >you can go > > ch+=16; > >so basicly you add or subtract 2 to the power of the bit you want. or, if you like assembler you can use "setb" and friends. in addition, OR'ing allows you to set a bit, and you can also AND a bit out with a mask. using logical bitwise operations are usually faster than adding and subtracting. e.g. a|=2; set bit #1 of 0-7 bits. a&=0xfd; clear bit #1 of 0-7 bits.