Breaking

मंगलवार, 14 अप्रैल 2020

BCA 2nd Sem Notes-Bitwise Operators

  • UNIT-I (Array)
Definition
Declaration & initialization of 1D 
Accessing array elements
Displaying array elements
Sorting arrays
Arrays and function
Declaration & initialization of 1D
Accessing and Displaying
Memory representation of array [Row Major, Column Major]
Multidimensional array





  • UNIT-II (Pointers)
  • Definition
    Declaration & initialization
    Indirection operator
    address of operator
    pointer arithmetic
    dynamic memory allocation
    arrays and pointers
    function and pointers



  • UNIT-III (Strings)








  • UNIT-V                                  (C Preprocessor)





  • UNIT-VI                                (File Handling)

  • Bitwise Operators

    Bitwise administrators are utilized for controlling a piece of information at the bit level, additionally called as bit-level programming. Bit-level programming primarily comprises of 0 and 1.


    OperatorMeaning
    &Bitwise AND operator
    |Bitwise OR operator
    ^Bitwise exclusive OR operator
    ~Binary One's Complement Operator is a unary operator
    <<Left shift operator
    >>Right shift operator
    The result of the computation of bitwise logical operators is shown in the table given below.
    xyx & yx | yx ^ y
    00000
    01011
    10011
    11110

    Bitwise AND

    This is one of the most commonly used logical bitwise operators. It is represented by a single ampersand sign (&). Two integer expressions are written on each side of the (&) operator.
    The result of the bitwise AND operation is 1 if both the bits have the value as 1; otherwise, the result is always 0.
    Let us consider that we have 2 variables op1 and op2 with values as follows:
    Op1 = 0000 1101
    Op2 = 0001 1001
    The result of the AND operation on variables op1 and op2 will be
    Result = 0000 1001
    
    As we can see, two variables are compared bit by bit. Whenever the value of a bit in both the variables is 1, then the result will be 1 or else 0.

    Bitwise OR

    It is represented by a single vertical bar sign (|). Two integer expressions are written on each side of the (|) operator.
    The result of the bitwise OR operation is 1 if at least one of the expression has the value as 1; otherwise, the result is always 0.
    Let us consider that we have 2 variables op1 and op2 with values as follows:
    p1 = 0000 1101
    p2 = 0001 1001
    The result of the OR operation on variables op1 and op2 will be
    Result = 0001 1101
    
    As we can see, two variables are compared bit by bit. Whenever the value of a bit in one of the variables is 1, then the result will be 1 or else 0.

    Bitwise Exclusive OR

    It is represented by a symbol (^). Two integer expressions are written on each side of the (^) operator.
     -OR operation is 1 if only one of the expression has the value as 1; otherwise, the result is always 0.
    Let us consider that we have 2 variables op1 and op2 with values as follows:
    p1 = 0000 1101
    p2 = 0001 1001
    The result of the OR operation on variables op1 and op2 will be
    Result = 0001 0100
    
    As we can see, two variables are compared bit by bit. Whenever only one variable holds the value 1 then the result is 0 else 0 will be the result.
    Let us write a simple program that demonstrates bitwise logical operators.
    #include <stdio.h>
    int main() {
    int a = 20; /* 20 = 010100 */
    int b = 21; /* 21 = 010101 */
    c = a & b; /* 20 = 010100 */
    int c = 0;
    c = a | b; /* 21 = 010101 */
    printf("AND - Value of c is %d\n", c );
    c = a ^ b; /* 1 = 0001 */
    printf("OR - Value of c is %d\n", c );
    }
    printf("Exclusive-OR - Value of c is %d\n", c );
    getch();
    Output:
    AND - Value of c is 20
    OR - Value of c is 21
    Exclusive-OR - Value of c is 1

    Bitwise shift operators

    The bitwise shift operators are used to move/shift the bit patterns either to the left or right side. Left and right are two shift operators provided by 'C' which are represented as follows:
    Operand << n (Left Shift)
    Operand >> n (Right Shift)
    Here,
    • an operand is an integer expression on which we have to perform the shift operation.
    • 'n' is the total number of bit positions that we have to shift in the integer expression.
    The left shift operation will shift the 'n' number of bits to the left side. The leftmost bits in the expression will be popped out, and n bits with the value 0 will be filled on the right side.
    The right shift operation will shift the 'n' number of bits to the right side. The rightmost 'n' bits in the expression will be popped out, and the value 0 will be filled on the left side.
    Example: x is an integer expression with data 1111. After performing shift operation the result will be:
    x << 2 (left shift) = 1111<<2 = 1100
    x>>2 (right shift) = 1111>>2 = 0011
    Shifts operators can be combined then it can be used to extract the data from the integer expression. Let us write a program to demonstrate the use of bitwise shift operators.
    #include <stdio.h>
    int main() {
    int a = 20; /* 20 = 010100 */
    int c = 0;
    printf("Left shift - Value of c is %d\n", c );
    c = a << 2; /* 80 = 101000 */
    printf("Right shift - Value of c is %d\n", c );
    c = a >> 2; /*05 = 000101 */ return 0;
    }
    Output:
    Left shift - Value of c is 80
    Right shift - Value of c is 5
    After performing the left shift operation the value will become 80 whose binary equivalent is 101000.
    After performing the right shift operation, the value will become 5 whose binary equivalent is 000101.

    Bitwise complement operator

    The bitwise complement is also called one's complement operator since it always takes only one value or an operand. It is a unary operator.
    When we perform complement on any bits, all the 1's become 0's and vice versa.
    If we have an integer expression that contains 0000 1111 then after performing bitwise complement operation the value will become 1111 0000.
    Bitwise complement operator is denoted by symbol tilde (~).
    Let us write a program that demonstrates the implementation of a bitwise complement operator.
    #include <stdio.h>
    int main() {
    int a = 10; /* 10 = 1010 */
    int c = 0; c = ~(a);
    printf("Complement - Value of c is %d\n", c );
    return 0;
    }
    Output:
    Complement - Value of c is -11
    Here is another program, with an example of all the operators discussed so far:
    #include <stdio.h>
    main() {
    unsigned int x = 48; /* 48 = 0011 0000 */
    unsigned int y = 13; /* 13 = 0000 1101 */
    z =x & y; /* 0 = 0000 0000 */
    int z = 0;
    z = x | y; /* 61 = 0011 1101 */
    printf("Bitwise AND Operator - x & y = %d\n", z );
    z= x^y; /* 61 = 0011 1101 */
    printf("Bitwise OR Operator - x | y = %d\n", z ); printf("Bitwise XOR Operator- x^y= %d\n", z);
    z = x << 2; /* 192 = 1100 0000 */
    z = ~x; /*-61 = 1100 0011 */ printf("Bitwise One's Complement Operator - ~x = %d\n", z);
    printf ("Bitwise Right Shift Operator x >> 2= %d\n", z );}
    printf("Bitwise Left Shift Operator x << 2= %d\n", z );
    z= x >> 2; /* 12 = 0000 1100 */
    After we compile and run the program, it produces the following result:
    Bitwise AND Operator - x & y = 0
    Bitwise OR Operator - x | y = 61
    Bitwise One's Complement Operator - ~x = -49
    Bitwise XOR Operator- x^y= 61
    Bitwise Right Shift Operator x >> 2= 12
    Bitwise Left Shift Operator x << 2= 192




    कोई टिप्पणी नहीं:

    एक टिप्पणी भेजें