#### Archived

This topic is now archived and is closed to further replies.

# bits -short and simple

This topic is 5963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 ?

##### Share on other sites
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

##### Share on other sites
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
// ...

##### Share on other sites
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);

##### Share on other sites
thank you sicman!
I think i get it :-)

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]

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633431
• Total Posts
3011834
• ### Who's Online (See full list)

There are no registered users currently online

×