Donate to Grow My Website (1 $ please)

  • You may choose specified posts to show it as featured posts with automatic sliding effects and keep visitors engaged for long time on your sites.
  • Dec 14, 2018

    Bit Algorithm - Learnengineeringforu

    Bit Algorithms


    Bit manipulation is the process of manipulating bits which are used to represent some word in memory .bit manipulation requires low level programming as we are completely working on the bits i.e 0,1. Application of the bit manipulation is in finding of errors and correcting them , compressing and encrypting data . All new generation programming languages will allow programmer to deal with abstractions which are user understandable rather than using bits which is machine understandable . Source code of the program which uses bit manipulation will use different bitwise operations: bitwise AND, bitwise OR, bitwise XOR, bitwise NOT, and bitwise bit shifts.

    Bitwise operations

    Any bitwise operation will operates either on one or more bits . It is the fastest and older approach used by the CPU .Such operations will manipulate the values for comparisons and calculations. Bitwise operations are machine understandable and hence faster. Bitwise operations will use less power because of least use of the resources.

    Bitwise Operators

    Bitwise operator used to work on bits there by performing bit-by-bit operation.

    Operator

    Description

    Example

    &

    Bitwise AND Operator will result 1 if both the bits are 1.

    A & B

    |

    Bitwise OR Operator will result in 1 if either both the bits are 1 of any of the one bit is 1 .

    A | B

    ^

    Bitwise XOR Operator results in 1 if one of the bits is 1 but not both.

    A ^ B

    ~

    Bitwise Complement will 'flip' bits resulting 0 in 1 and 1 in 0.

    ~A

    <<

    Bitwise Left Shift Operator will shift the bits specified to left

    A << 2

    >>

    Bitwise Right Shift Operator will shift the bits specified to right.

    A >> 2

    Bitwise operator AND ( &)

    bit Abit BA & B
    000
    010
    100
    111







    Bitwise AND operation of two integers 13 and 24.
    13 = 00001101 (In Binary)
    24 = 00011000 (In Binary)
    math

    Bitwise operator OR ( | )

    OperatorDescriptionExample
    &Bitwise AND Operator will result 1 if both the bits are 1.A & B
    |Bitwise OR Operator will result in 1 if either both the bits are 1 of any of the one bit is 1 .A | B
    ^Bitwise XOR Operator results in 1 if one of the bits is 1 but not both.A ^ B
    ~Bitwise Complement will 'flip' bits resulting 0 in 1 and 1 in 0.~A
    <<Bitwise Left Shift Operator will shift the bits specified to leftA << 2
    >>Bitwise Right Shift Operator will shift the bits specified to right.A >> 2


















    Bitwise operator AND ( &)

    bit Abit BA| B
    000
    011
    101
    111







    13 = 00001101 (In Binary)
    24 = 00011000 (In Binary)
    Bitwise OR Operation of 13 and 24
    math

    Bitwise operator XOR ( ^)

    OperatorDescriptionExample
    &Bitwise AND Operator will result 1 if both the bits are 1.A & B
    |Bitwise OR Operator will result in 1 if either both the bits are 1 of any of the one bit is 1 .A | B
    ^Bitwise XOR Operator results in 1 if one of the bits is 1 but not both.A ^ B
    ~Bitwise Complement will 'flip' bits resulting 0 in 1 and 1 in 0.~A
    <<Bitwise Left Shift Operator will shift the bits specified to leftA << 2
    >>Bitwise Right Shift Operator will shift the bits specified to right.A >> 2


















    Bitwise operator AND ( &)

    bit Abit BA ^ B
    000
    011
    101
    110








    13 = 00001101 (In Binary)
    24 = 00011000 (In Binary)
    Bitwise XOR Operation of 13 and 24
    math

    Bitwise complement "~"

    bit A~A
    01
    10





    13 = 00001101 (In Binary)
    Bitwise complement of 13
    Complement of 00001101 is 11110010

    Shift Operators

    There are two bit shifting operators :
    • Right shift
    • Left shift

    Right Shift Operator

    13 = 00001101 (In Binary)
    13>>3 = 00000001 (In binary) [Right shifting bits by three bits]

    Left Shift Operator

    13 = 00001101 (In Binary)
    
    13<<2 = 00110100(In binary) [left shifting bits left by two bit]
    
    13<<0 =00001101  (Shift by 0)
    

    Example:-

    #include
    int main()
    {
    unsigned char p = 5, q = 9; // p = 4(00000101), q = 8(00001001)
    printf("p = %d, q = %d\n", p, q);
    printf("p&q= %d\n", p&q); 
    printf("p|q = %d\n", p|q);  
    printf("p^q = %d\n", p^q); 
    printf("~p = %d\n", p = ~p);   
    printf("q<<1 = %d\n", q<<1);  
    printf("q>>1 = %d\n", q>>1);  
    return 0;
    }
    

    Output:

    p = 5, q = 9
    
    p&q = 1
    
    p|q = 13
    
    p^q = 12
    
    ~p = 250
    
    q<<1 = 18
    
    q>>1 = 4
    
    

    Sample Tutorial Problem




    Any positive integer can be represented by a four digit binary number. For example 0 is represented by binary 0000, 1 is represented by 0001, 2 is represented by 0010 and so on. In this binary representation a bit is called set if it is 1. You need to write a program to input an integer and find next greater integer who has one more number of set bits in its binary representation.

    Input Format
    It will be an integer number n.
    Constraints
    0 <= n <= 100
    Output Format
    It will an integer number greater than n having one more set bits in binary representation than n.
    Sample TestCase 1
    Input
    10
    Output
    11
    1
    2
    3
    4
    5
    6
    7
    /* Read input from STDIN. Print your output to STDOUT*/
    #include<stdio.h>
    int main(int argc, char *a[])
    {
    //Write code here
    }

    No comments:
    Write Comments