An introduction to bitwise operators

Discussion in 'C' started by Sanskruti, Feb 6, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits. On many computers, bitwise operations are slightly faster than addition and subtraction operations and significantly faster than multiplication and division operations.

    Bitwise operators



    NOT



    A bitwise NOT or complement is a unary operation which performs logical negation on each bit. 0 digits become 1, and vice versa.

    For example:
    NOT 0111 = 1000

    In the C and C++ programming languages, the bitwise NOT operator is "~" (tilde).

    For example:
    x = ~y;
    assigns x the result of "NOT y".

    This is different from the C and C++ logical "not" operator, "!" (exclamation point), which treats the entire numeral as a single Boolean value.

    For example:

    x = !y;

    assigns x a Boolean value of "true" if y is "false", or "false" if y is "true". In C and C++, a numerical value is interpreted as "true" if it is non-zero

    Bitwise NOT is useful in finding the one's complement of a binary numeral, and may be an intermediate step in finding the two's complement of the same numeral.

    OR



    A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits and performing the logical OR operation on each pair of corresponding bits. In each pair, the result is 1 if either the first bit is 1 OR the second bit is 1. Otherwise, the result is zero.

    For example:
    0101 OR 0011 = 0111

    In C and C++, the bitwise OR operator is "|"

    For example:
    x = y | z;

    assigns x the result of "y OR z".

    This is different from the C and C++ logical "or" operator, "||" (two vertical bars), which treats its operands as Boolean values, and results in "true" or "false".

    XOR



    A bitwise exclusive OR takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same.

    For example:
    0101 XOR 0011= 0110

    In C, and C++, the bitwise XOR operator is "^" .

    For example:
    x = y ^ z;
    assigns x the result of "y XOR z".

    AND



    A bitwise AND takes two binary representations of equal length and performs the logical AND on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is zero.

    For example:
    0101 AND 0011= 0001

    In C and C++, the bitwise AND operator is “&”

    For example:

    x = y & z;

    assigns x the result of "y AND z".

    This is different from the C and C++ logical "and" operator, "&&", which takes two logical operands as input and produces a result of "true" or "false".

    The bitwise AND may be used to perform a bit mask operation. This operation may be used to isolate part of a string of bits, or to determine whether a particular bit is 1 or 0.

    For example, given a bit pattern:
    0011

    To determine whether the third bit is 1, a bitwise AND is applied to it along with another bit pattern containing 1 in the third bit, and 0 in all other bits:

    0011 AND 0010= 0010

    Since the result is 0010 (not zero), the third bit in the original pattern was 1.
    The bitwise AND can also be combined with the bitwise NOT to clear bits.

    Bit shift



    The bit shift is sometimes considered a bitwise operation, since it operates on a set of bits. Unlike the above, the bit shift operates on the entire numeral, rather than on individual bits. In this operation, the digits are moved, or shifted, to the left or right.

    Shifts in C, C++



    In C, C++ and many other languages that borrow syntax from them, the left and right shift operators are "<<" and ">>", respectively. The number of places to shift is given as an argument to the shift operators.

    For example:
    x = y << 2;

    assigns x the result of shifting y to the left by two digits. In C and C++ when shifting right if the type of y is unsigned then a logical shift is used, if the type of y is signed then the behaviour for negative numbers is undefined by the standard.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    An interesting article.
     
  3. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice