bits -short and simple

Started by
8 comments, last by uncutno 21 years, 8 months ago
How do you find out if for example bit 3 in a byte is 0 or 1 ? And how do you set for example bit 3 in a byte to 0 or 1 ?
-Anders-Oredsson-Norway-
Advertisement
if(bits&(1<<3))
{
the 3rd bit in BITS ==1
}


//set to zero
if(bits&(1<<3))
bits=bits|(1<<3);
//set to 1
if(!(bits&(1<<3)))
bits=bits|(1<<3);

if i remember correctly, that is how you do it

--sicman
--the to lazy to register type of programmer
Assuming you mean bit 3 as in 3210 (<- a register, for example)
do this:

#define YOURVAR_BIT3SET 0xFF /* ALL BITS SET OF COURSE */
#define MYCHECK_FLAG 0x08

if( YOURVAR_BIT3SET & MYCHECK_FLAG)
// DO STUFF
// ...
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Your clearing code is the same as your setting code, and the extra instruction to check if it''s already set or clear isn''t necessary.

set bit three: bits |= 1<<3;
clear bit three: bits &= ~(1<<3);
thank you sicman!
I think i get it :-)
newer knew about the | thing..

Like2Byte: where in your example are you suposed to enter the byte you want to modify/test ?

Stoffel:
GREAT! :-) if you mind, could you parshe the symbols to logic in english? (simple) i get the 1<<3(no need) , but the &= and ~ ???


[edited by - uncutno on July 25, 2002 11:32:53 AM]
-Anders-Oredsson-Norway-
quote:Original post by Anonymous Poster
if(bits&(1<<3))
{
the 3rd bit in BITS ==1
}


//set to zero
if(bits&(1<<3))
bits=bits|(1<<3);
//set to 1
if(!(bits&(1<<3)))
bits=bits|(1<<3);

if i remember correctly, that is how you do it

--sicman
--the to lazy to register type of programmer


Your set to one and set to zero perform the same function: "bits = bits | (1<<3) ;". So one of them must be incorrect. To set to zero you''re going to need an AND function with all ones except for a zero in the 3rd position.
quote:Original post by Stoffel
Your clearing code is the same as your setting code, and the extra instruction to check if it''s already set or clear isn''t necessary.

set bit three: bits |= 1<<3;
clear bit three: bits &= ~(1<<3);


i thought that ORing outputted 0 when both bits were 1''s...but maybe that is XORING, i cant remeber...

also, what does the ~ do in your clearing bit code?
OR returns true if either is true, you are thinking of XOR, which only returns true if one is true and the other is false.

&= is just like +=, -=, etc. A &= B expands out to A = A & B.

~ is a bitwise not...it goes through and swaps each bit, so that 1001 becomes 0110. ~(1<<3) == ~(100) == (011). So in conjunction with the &=, every bit on the RHS remains the same except the third, which is turned off, because it is 0 on the LHS.

CM
cool, makes sense to me. that just might help speed up some of my crappy bit code...off to optomize


BTW...i decided to stop being lazy and register...woohoo for me
______________blah
Like2Byte (11:39 AM) :
quote:Original post by uncutno

Like2Byte: where in your example are you suposed to enter the byte you want to modify/test ?

[edited by - uncutno on July 25, 2002 11:31:01 AM]


Honestly, I think my example was not a model answer of what you are trying to find out. It actually didn''t. Sorry.

I used mine as a flag check.


  #define CHECK_SN          0x0001#define NOCHECK_SN        0x0002#define REPORT_LAZINESS   0x0004//declarationbool read_product_profile( product_t * p, int flags);// definitionread_product_profile( product_t * p, int flags){ if ( flags & CHECK_SN )    verify_sn( p, p->serial_num ); if (flags & REPORT_LAZINESS )    report_employee( p->emp ); // or something // of course, NOCHECK_SN would be in here also I just couldn''t think of anything particularly useful for this demo on it.  Populate_database(p); return 1;}// usageif (read_product_profile( Porche, CHECK_SN | REPORT_LAZINESS ) ){ MessageBox("Shipment Added");  // other stuff}  


ps: sorry for the delay. The server kept kicking me and giving me a timeout from GD''s server.
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.

This topic is closed to new replies.

Advertisement