Raised This Month: $ Target: $400
 0% 

Is small like C++


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 07-17-2004 , 10:50  
Reply With Quote #7

I have some things to add :
Code:
if (a)
means
Code:
if (a != 0)

and

Code:
if (!a)
means
Code:
if (a == 0)

When we are talking about operators:
All stuff about bit setting / shifting / checking / ...

For ilustration i will only use 8 bits:
Code:
// .....   name  value     bit value #define BIT_1 1        // 00000001 #define BIT_2 2        // 00000010 #define BIT_3 4        // 00000100 #define BIT_4 8        // 00001000 #define BIT_5 16       // 00010000 #define BIT_6 32       // 00100000 #define BIT_7 64       // 01000000 #define BIT_8 128      // 10000000
Then you can use the LOGICAL OR ( '|' ) and LOGICAL AND ( '&' ).

LOGICAL OR:
Basically loops through all bits of two integers and creates a new integer in this way:
Code:
Integer1 01001010 Integer2 11000100 Result   11001110
So the bit in result will be set to 1 if the same bit in Integer1 _OR_ in Integer2 is 1

LOGICAL AND:
Basically loops through all bits of two integers and creates a new integer in this way:
Code:
Integer1 01001010 Integer2 11000100 Result   01000100
This will only set a bit to 1 when the same bit is 1 in Integer1 _AND_ Integer2.

BUT:
x && y
does basically
(x!=0) & (y != 0)
( same with || )

When u write
Code:
x |= y
it could be translated (=it does the same thing) to this:
Code:
x = x | y

You can do the same with '&' ( -> 'x &= y' )

Now you can use these operators to make a "bit sum".
Code:
new a = 0;   // 00000000 a |= BIT_3   // 00000100 // means: //    a = a | BIT_3 // -> a = 00000000 | 00000100 = 00000100 a |= BIT_7 // means: //    a = a | BIT_3 // -> a = 00000100 | 01000000 = 01000100

Now we have an 8-byte integer a in which the third and the seventh bits are set.

We want to check which bits are set. So we will do:
Code:
// Check // BIT_3 if ((a & BIT_3) != 0) {      // BIT_3 is set }
it works like this:
Code:
if ((a & BIT_3) != 0) // if ((01000100 & 00000100) != 0) // We know that 01000100 & 00000100 = 00000100 // so it isn't 0 when BIT_3 is set.
Now let's check BIT_5
Code:
// Check // BIT_5 if ((a & BIT_5) != 0) {     // BIT_5 is set }
=
Code:
if ((a & BIT_5) != 0) // if ((01000100 & 00010000) != 0) // We know that 01000100 & 00010000 = 00000000 // so it isn 0 when BIT_5 is not set.

bit shift left:
Code:
10001001  <<  1 = 00010010
And
Code:
10001001  <<  2 = 00100100
could be written as
Code:
(10001001  <<  1) << 1 = 00100100

bit shift right: The same thing, other direction

So u can use 1 << x to generate values like 1, 2, 4, 8, 16, 32, ...
__________________
hello, i am pm
PM is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode