Computer Science‎ > ‎

C Program: Demonstrating the use of Bitwise Operators



 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart


          
C How to Program (Deitel and Deitel)

 



C Program demonstrating the use of Bitwise Operators


/*This program demonstartes the bitwise operations OR,XOR,NOT,AND,LEFT SHIFT and RIGHT SHIFT.*/
#include<stdio.h>
int main()
{
    int num1,num2,resOR,resAND,resXOR,resNOT,resLSHIFT,resRSHIFT;
    printf("This is program to demonstrate the results of bitwise OR,AND,NOT,XOR,LEFT SHIFT and RIGHT SHIFT operations \n");
    printf("Enter the numbers on which the operations are to be performed\n");
    scanf("%d%d",&num1,&num2);
    /*In the OR operatrion the numbers are represented by the computer in binary form and then
    Logical OR operation is performed on each subsequent pair.The resulting binary number is converted in decimal form and is the result.*/
    resOR=num1|num2;
    printf("The result of bitwise OR operation is %d\n",resOR);
    /*In the AND operatrion the numbers are represented by the computer in binary form and then
    Logical AND operation is performed on each subsequent pair.The resulting binary number is converted in decimal form and is the result.*/
    resAND=num1&num2;
    printf("The result of bitwise AND operation is %d\n",resAND);
    /*In the NOT operatrion the number is represented by the computer in binary form and then
    each bit is inverted.The resulting binary number is converted in decimal form and is the result.*/
    resNOT=~num1;
    printf("The result of bitwise NOT operation on %d is %d\n",num1,resNOT);
    resNOT=~num2;
    printf("The result of bitwise NOT operation on %d is %d\n",num2,resNOT);
    //Logical XOR operation is performed on the indivisual pair of bits.
    resXOR=num1^num2;
    printf("The result of bitwise XOR operation is %d\n",resXOR);
    resLSHIFT=num1<<1;    //The bits are shifted left wards by one place and the MSD occupies the LSD
    printf("The result of bitwise LEFT SHIFT by 1 bits operation on %d is %d\n",num1,resLSHIFT);
    resLSHIFT=num2<<1;
    printf("The result of bitwise LEFT SHIFT by 1 bits operation on %d is %d\n",num2,resLSHIFT);
    resRSHIFT=num1>>1; //The bits are shifted right wards by one place and the MSD occupies its place unchanged
    printf("The result of bitwise RIGHT SHIFT by 1 bits operation on %d is %d\n",num1,resRSHIFT);
    resRSHIFT=num2>>1;
    printf("The result of bitwise RIGHT SHIFT by 1 bits operation on %d is %d\n",num2,resRSHIFT);
    getch();
}
/*A sample run of the program was carried out and the results were found as:-
This is program to demonstrate the results of bitwise OR,AND,NOT,XOR,LEFT SHIFT and RIGHT SHIFT operations
Enter the numbers on which the operations are to be performed
2 3
The result of bitwise OR operation is 3
The result of bitwise AND operation is 2
The result of bitwise NOT operation on 2 is -3
The result of bitwise NOT operation on 3 is -4
The result of bitwise XOR operation is 1
The result of bitwise LEFT SHIFT by 1 bits operation on 2 is 4
The result of bitwise LEFT SHIFT by 1 bits operation on 3 is 6
The result of bitwise RIGHT SHIFT by 1 bits operation on 2 is 1
The result of bitwise RIGHT SHIFT by 1 bits operation on 3 is 1
*/







 













C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers