Archived

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

bits -short and simple

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest 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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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

//declaration

bool read_product_profile( product_t * p, int flags);

// definition

read_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;
}

// usage

if (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.

Share this post


Link to post
Share on other sites