Check The Programming Section
Operator precedence helps the compiler to decide the order of evalution when an expression have multiple operators. On the other side, associativity the direction of the execution when an expression have multiple operators with same precedence. Consider the example below:
int k = 4 + 5 / 6 && 3<4 + 2;
The above expression have multiple operators of types arithmetic, logical and relational. And out of all operators, arithmetic operators has higher precedence. But the expression also have two binary plus (+), and divission (/) operator. Let's parenthesis the above expression according to precedence and associativity
k = ((4 + (5 / 6)) && (3<(4 + 2)));
The direction of associativity for all these operators is left to right and the division operator (/) has the higher precedence. First divission operation is performed first and then result get added with 4.
k = ((4 + 0) && (3<(4 + 2)));
After addition of 4 with 0, addition between 4 and 2 gets executed and the result get compared with 3 and returns the relationship as true (1) as follows:
k = (4 && 1);
4 and 1 both is true and the outcome of the logical AND operation will returns 1 as true relationships to k.
If we want to right shift a by b as a>>b and b is negative, then the result is unpredictable.
If we want a>>b, and a in negative then its sign bit would be 1. In some computers, right shifting a negative number would result in extending the sign bit. For example, if a contains -1, its binary representation would be 1111111111111111. Without sign extension, the operation a >> 4 would be 0000111111111111. However, on the machine on which we executed this expression the result turns out to be 1111111111111111. Thus the sign bit 1 continues to get extended.
Purposefully, the logical & operator is used for two purpose only
It is used to check whether a particular bit in a given number is ON or OFF.
It is used to switch OFF a particular bit in a number.
To switch OFF a particular bit in a given number we will use ~, << and & operators. We will use expression ~(1 << (k – 1)) (mask), and it will return us a number whos all bits are set except the kth bit, that we want to switch OFF of a given number. Then switch off the kth bit we will do the ANDing of the given number by the mask value. Consider the below source code:
int main(){
short int num = 25;
printf("The %d in binary is ",num);
getBits(num);
//set the 4th bit to OFF
short int k = 4;
//calculate the mask
short int mask = ~(1<<(k-1));
//switching of 4th bit by ANDing
num = num & mask;
printf("\nAfter switching of the 4th bit ");
getBits(num);
}
Note: The required code for getBits() is explained here.
To ON a particular bit in a given number we will use operators like bitwise << and |. Suppose we want to ON the 7th bit of a given number (bit numbering is started from 1), then the required OR mask is 1<<(k-1). k represents the given bit position and the required code is:
int main(){
short int num = 25;
printf("The %d in binary is ",num);
getBits(num);
//set the 7th bit to OFF
short int k = 7;
//calculate the mask
short int mask = (1<<(k-1));
//switching of 7th bit by ANDing
num = num | mask;
printf("\nAfter switching of the 4th bit ");
getBits(num);
}