Numerical representation of FALSE is zero. Numerical representation of TRUE is either one or minus one, or any other non-zero standard.<br><br>When you're doing:<br><br>if(flag){}<br><br>you're really checking if the flag variable is non-zero, upon which the if statement is executed.<br>This is equivalent to the following statement:<br><br>if( flag != 0 )<br><br>In C, true and false are defined as:<br><br>#define TRUE 1<br>#define FALSE 0<br><br>whereas there is a defined type Boolean<br><br>typedef int Boolean;<br><br>Bitwise operators are used for accessing certain bits in a numerical entity. It's used in different areas, some of them being areas where you need to access only a certain bit (hardware registers), or bit per bit accessing a file format structure, or transformation of input data, or for example conversion from big endian to little endian format. <br><br>Ofcourse, there are millions of instances where you use bitwise operations, so I suggest you get a hang of it pretty soon
<br><br>Say, you have a longword (32bits) and you only want the lowest 8 bits (byte). Look at this C snippet showing the & (AND) operator:<br><br>long a = 0xBAADF00D;<br>unsigned char b;<br><br>b = a & 0xFF; // 0x000000FF<br><br><br>Variable b will now contain 0x0D.<br><br>>> is the bitwise shift right operator. It will shift the bitmask as many positions as you want to the right. << does the same to the left.<br><br>b = (a & 0xFF0000) >> 16;<br><br>will give b the value 0xAD. First the paranthesis will result in 0x00AD0000 and then the shift to right will result in 0x000000AD.<br><br><< and >> also have the effect of multiplying or diving by the power of 2 multiples.<br><br>a = 6;<br>a = a << 1; // times 2<br><br>a equals now 12;<br><br>a= 5;<br>a = a << 2; // times 4<br><br>a equals now 20.<br><br>a = 16;<br>a = a >> 3; // divided by 8<br><br>a equals 2.<br><br>the | (OR) operator is used for setting bits (AND was used for clearing bits):<br><br>unsigned int a = 0xFF00;<br>a = a | 0xAC;<br><br>a equals now 0xFFAC.<br><br>Bits that are already set, remain set.<br><br>The ~ (NOT) operator isused to reverse the bits from zero to 1 and viceversa.<br><br>a = 0x00FF00FF;<br>a = ~a<br><br>a equals 0xFF00FF00.<br><br>Let me see if you understood these things...<br>What is the value of a after these statements:<br><br>long a = 0x55AA55AA;<br>long b = 0xBAADF00D;<br><br>a = (a & b) | (~a >> 16);<br><br>Hope I haven't been a pain in the ass
<br>
PDAFantast.