Search This Blog

About Me

My photo
Hello, my name is Gurnoor Singh. I am the creator of NoorEDU, a blog for about education . I live in India. I study in BCA Trade.In my free time, I enjoy hiking, practicing photography, and exploring the city by bike.

Wednesday 23 September 2020

Bitwise Operator in C

 

Bitwise Operator in C

The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster.

We have different types of bitwise operators in the C programming language. The following is the list of the bitwise operators:

OperatorMeaning of operator
&Bitwise AND operator
|Bitwise OR operator
^Bitwise exclusive OR operator
~One's complement operator (unary operator)
<<Left shift operator
>>Right shift operator

Let's look at the truth table of the bitwise operators.

XYX&YX|YX^Y
00000
01011
10011
11111

Bitwise AND operator

Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0.

For example,

  1. We have two variables a and b.  
  2. a =6;  
  3. b=4;  
  4. The binary representation of the above two variables are given below:  
  5. a = 0110  
  6. b = 0100  
  7. When we apply the bitwise AND operation in the above two variables, i.e., a&b, the output would be:  
  8. Result = 0100  

As we can observe from the above result that bits of both the variables are compared one by one. If the bit of both the variables is 1 then the output would be 1, otherwise 0.

Let's understand the bitwise AND operator through the program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=6, b=14;  // variable declarations  
  5.    printf("The output of the Bitwise AND operator a&b is %d",a&b);  
  6.    return 0;  
  7. }  

In the above code, we have created two variables, i.e., 'a' and 'b'. The values of 'a' and 'b' are 6 and 14 respectively. The binary value of 'a' and 'b' are 0110 and 1110, respectively. When we apply the AND operator between these two variables,

a AND b = 0110 && 1110 = 0110

Output

Bitwise Operator in C

Bitwise OR operator

The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are written on both sides of the (|) symbol. If the bit value of any of the operand is 1, then the output would be 1, otherwise 0.

For example,

  1. We consider two variables,  
  2. a = 23;  
  3. b = 10;  
  4. The binary representation of the above two variables would be:  
  5. a = 0001 0111  
  6. b = 0000 1010  
  7. When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be:  
  8. Result = 0001 1111  

As we can observe from the above result that the bits of both the operands are compared one by one; if the value of either bit is 1, then the output would be 1 otherwise 0.

Let's understand the bitwise OR operator through a program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=23,b=10;  // variable declarations  
  5.    printf("The output of the Bitwise OR operator a|b is %d",a|b);  
  6.    return 0;  
  7. }  

Output

Bitwise Operator in C

Bitwise exclusive OR operator

Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on both sides of the exclusive OR operator. If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0.

For example,

  1. We consider two variables a and b,  
  2. a = 12;  
  3. b = 10;  
  4. The binary representation of the above two variables would be:  
  5. a = 0000 1100  
  6. b = 0000 1010  
  7. When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be:  
  8. Result = 0000 1110  

As we can observe from the above result that the bits of both the operands are compared one by one; if the corresponding bit value of any of the operand is 1, then the output would be 1 otherwise 0.

Let's understand the bitwise exclusive OR operator through a program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=12,b=10;  // variable declarations  
  5.    printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);  
  6.    return 0;  
  7. }  

Output

Bitwise Operator in C

Bitwise complement operator

The bitwise complement operator is also known as one's complement operator. It is represented by the symbol tilde (~). It takes only one operand or variable and performs complement operation on an operand. When we apply the complement operation on any bits, then 0 becomes 1 and 1 becomes 0.

For example,

  1. If we have a variable named 'a',  
  2. a = 8;  
  3. The binary representation of the above variable is given below:  
  4. a = 1000  
  5. When we apply the bitwise complement operator to the operand, then the output would be:  
  6. Result = 0111  

As we can observe from the above result that if the bit is 1, then it gets changed to 0 else 1.

Let's understand the complement operator through a program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=8;  // variable declarations  
  5.    printf("The output of the Bitwise complement operator ~a is %d",~a);  
  6.    return 0;  
  7. }  

Output

Bitwise Operator in C

Bitwise shift operators

Two types of bitwise shift operators exist in C programming. The bitwise shift operators will shift the bits either on the left-side or right-side. Therefore, we can say that the bitwise shift operator is divided into two categories:

  • Left-shift operator
  • Right-shift operator

Left-shift operator

It is an operator that shifts the number of bits to the left-side.

Syntax of the left-shift operator is given below:

  1. Operand << n  

Where,

Operand is an integer expression on which we apply the left-shift operation.

n is the number of bits to be shifted.

In the case of Left-shift operator, 'n' bits will be shifted on the left-side. The 'n' bits on the left side will be popped out, and 'n' bits on the right-side are filled with 0.

For example,

  1. Suppose we have a statement:  
  2. int a = 5;  
  3. The binary representation of 'a' is given below:  
  4. a = 0101  
  5. If we want to left-shift the above representation by 2, then the statement would be:   
  6. a << 2;  
  7. 0101<<2 = 00010100  

Let's understand through a program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a=5; // variable initialization  
  5.     printf("The value of a<<2 is : %d ", a<<2);  
  6.     return 0;  
  7. }  

Output

Bitwise Operator in C

Right-shift operator

It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given below:

  1. Operand >> n;  

Where,

Operand is an integer expression on which we apply the right-shift operation.

N is the number of bits to be shifted.

In the case of the right-shift operator, 'n' bits will be shifted on the right-side. The 'n' bits on the right-side will be popped out, and 'n' bits on the left-side are filled with 0.

For example,

  1. Suppose we have a statement,  
  2. int a = 7;  
  3. The binary representation of the above variable would be:  
  4. a = 0111  
  5. If we want to right-shift the above representation by 2, then the statement would be:  
  6. a>>2;  
  7. 0000 0111 >> 2 = 0000 0001  

Let's understand through a program.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a=7; // variable initialization  
  5.     printf("The value of a>>2 is : %d ", a>>2);  
  6.     return 0;  
  7. }  

Output

Bitwise Operator in C

Conditional Operator in C

 

Conditional Operator in C

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.

As conditional operator works on three operands, so it is also known as the ternary operator.

The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.

Syntax of a conditional operator

  1. Expression1? expression2: expression3;  

The pictorial representation of the above syntax is shown below:

Conditional Operator in C

Meaning of the above syntax.

  • In the above syntax, the expression1 is a Boolean condition that can be either true or false value.
  • If the expression1 results into a true value, then the expression2 will execute.
  • The expression2 is said to be true only when it returns a non-zero value.
  • If the expression1 returns false value then the expression3 will execute.
  • The expression3 is said to be false only when it returns zero value.

Let's understand the ternary or conditional operator through an example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int age;  // variable declaration  
  5.     printf("Enter your age");  
  6.     scanf("%d",&age);   // taking user input for age variable  
  7.     (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));  // conditional operator  
  8.     return 0;  
  9. }  

In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the condition by using a conditional operator. In this condition, we are checking the age of the user. If the age of the user is greater than or equal to 18, then the statement1 will execute, i.e., (printf("eligible for voting")) otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).

Let's observe the output of the above program.

If we provide the age of user below 18, then the output would be:

Conditional Operator in C

If we provide the age of user above 18, then the output would be:

Conditional Operator in C

As we can observe from the above two outputs that if the condition is true, then the statement1 is executed; otherwise, statement2 will be executed.

Till now, we have observed that how conditional operator checks the condition and based on condition, it executes the statements. Now, we will see how a conditional operator is used to assign the value to a variable.

Let's understand this scenario through an example.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int a=5,b;  // variable declaration  
  5.    b=((a==5)?(3):(2)); // conditional operator  
  6.    printf("The value of 'b' variable is : %d",b);  
  7.     return 0;  
  8. }  

In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a' variable. After the declaration, we are assigning value to the 'b' variable by using the conditional operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value otherwise 2.

Output

Conditional Operator in C

The above output shows that the value of 'b' variable is 3 because the value of 'a' variable is equal to 5.

As we know that the behavior of conditional operator and 'if-else' is similar but they have some differences. Let's look at their differences.

  • A conditional operator is a single programming statement, while the 'if-else' statement is a programming block in which statements come under the parenthesis.
  • A conditional operator can also be used for assigning a value to the variable, whereas the 'if-else' statement cannot be used for the assignment purpose.
  • It is not useful for executing the statements when the statements are multiple, whereas the 'if-else' statement proves more suitable when executing multiple statements.
  • The nested ternary operator is more complex and cannot be easily debugged, while the nested 'if-else' statement is easy to read and maintain.

Compile time vs Runtime

 

Compile time vs Runtime

Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running. Both the compile-time and runtime refer to different types of error.


Compile-time errors are the errors that occurred when we write the wrong syntax. If we write the wrong syntax or semantics of any programming language, then the compile-time errors will be thrown by the compiler. The compiler will not allow to run the program until all the errors are removed from the program. When all the errors are removed from the program, then the compiler will generate the executable file.

The compile-time errors can be:

  • Syntax errors
  • Semantic errors

Syntax errors

When the programmer does not follow the syntax of any programming language, then the compiler will throw the syntax error.

For example,

int a, b:

The above declaration generates the compile-time error as in C, every statement ends with the semicolon, but we put a colon (:) at the end of the statement.

Semantic errors

The semantic errors exist when the statements are not meaningful to the compiler.

For example,

a+b=c;

The above statement throws a compile-time errors. In the above statement, we are assigning the value of 'c' to the summation of 'a' and 'b' which is not possible in C programming language as it can contain only one variable on the left of the assignment operator while right of the assignment operator can contain more than one variable.

The above statement can be re-written as:

c=a+b;

Runtime errors

The runtime errors are the errors that occur during the execution and after compilation. The examples of runtime errors are division by zero, etc. These errors are not easy to detect as the compiler does not point to these errors.

Let's look at the differences between compile-time and runtime:

Compile-timeRuntime
The compile-time errors are the errors which are produced at the compile-time, and they are detected by the compiler.The runtime errors are the errors which are not generated by the compiler and produce an unpredictable result at the execution time.
In this case, the compiler prevents the code from execution if it detects an error in the program.In this case, the compiler does not detect the error, so it cannot prevent the code from the execution.
It contains the syntax and semantic errors such as missing semicolon at the end of the statement.It contains the errors such as division by zero, determining the square root of a negative number.

Example of Compile-time error

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a=20;  
  5.     printf("The value of a is : %d",a):  
  6.     return 0;  
  7. }  

In the above code, we have tried to print the value of 'a', but it throws an error. We put the colon at the end of the statement instead of a semicolon, so this code generates a compile-time error.

Output

Compile time vs Runtime

Example of runtime error

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a=20;  
  5.     int b=a/0; // division by zero  
  6.     printf("The value of b is : %d",b):  
  7.     return 0;  
  8. }  

In the above code, we try to divide the value of 'b' by zero, and this throws a runtime error.

Output

Compile time vs Runtime

Bitwise Operator in C