I do not know how to refer to bits in C and i need an example.Do i refer as char or as int.I do not know basic commands in bits.
I just need an example...
I do not know how to refer to bits in C and i need an example.Do i refer as char or as int.I do not know basic commands in bits.
I just need an example...
http://www.cprogramming.com/tutorial...operators.html does a pretty damn good job.
Last edited by zacs7; 07-10-2007 at 06:51 AM.
You should read up about 'bitwise operators' in C. There should be a tutorial here about it.
For example if you want to check if the 32 bit in a byte is on you could use the bitwise 'and' opetator:
For setting a bit I would check if its set first, if not then just add the bit value to it.Code:if(somenumber & 32)
This may not be the best way to do this tho.Code:if(!(somenumber & 32))somenumber+=32;
The tutorial covers all that in depth, and explains there's 8 bits to a byte, usually
Last edited by zacs7; 07-10-2007 at 07:28 AM.
Er... I said 32 bit, not 32nd, but I guess it could seem confusing. I usually talk like that anyway with binary stuff and break ints down into octects. Plus it meant that I dident have to go into bitshifting with my explanation. But yeah, i'm sure the tutorial is better than my vague explanation.
Ohh, I see where your coming from. Checking if the 32nd bit is on in a set of bytes, ie a byte (if you break it down)?
Or or:For setting a bit I would check if its set first, if not then just add the bit value to it.
This may not be the best way to do this tho.Code:if(!(somenumber & 32))somenumber+=32;
Code:somenumber |= 32;
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Yeah, ORing its better. I forgot that
That bit would still be in one byte, as 32 in this case would be bit 5 (starting from bit 0).
I get it now.... I just never think of the 5th (or 6th) bit as the "32 bit". lol....
>I get it now.... I just never think of the 5th (or 6th) bit as the "32 bit". lol....
Me either. I would certainly use hex to write the same expression:
Or do a bit shift.Code:if (somenumber & 0x20)