Is it very important to know bitwise operator well?

Discussion in 'C Programming' started by Junmin H., Sep 13, 2007.

  1. Junmin H.

    Junmin H. Guest

    Hello, all. I never use them when I am programming.
    But i have been reading codes that using them.
    When should I use them? Why not use char/short/int/long?
    Is it very important to know them well? Because it's binary,
    I found that it's a little difficult to understand then decimal.

    Thanks.

    Junmin H.
     
    Junmin H., Sep 13, 2007
    #1
    1. Advertising

  2. Junmin H.

    Guest

    On Sep 13, 2:59 pm, "Junmin H." <> wrote:
    > Hello, all. I never use them when I am programming.
    > But i have been reading codes that using them.
    > When should I use them? Why not use char/short/int/long?
    > Is it very important to know them well? Because it's binary,
    > I found that it's a little difficult to understand then decimal.
    >
    > Thanks.
    >
    > Junmin H.


    IMO, it is very important to know them, as you are sure to come across
    them again in the future. I think the root of the problem is that you
    find it hard to understand binary numbers. If you are going to be a
    half-decent programmer (ie be doing it as a profession), I recommend
    that you address this issue immediately. Use Google, there are plenty
    of resources on the Internet that discuss binary arithmetic. While
    you are doing this, find out about octal and hexadecimal numbers are
    as well.

    Regards,
    B.
     
    , Sep 13, 2007
    #2
    1. Advertising

  3. "Junmin H." <> wrote in message
    news:fcag3r$qia$...
    > Hello, all. I never use them when I am programming.
    > But i have been reading codes that using them.
    > When should I use them? Why not use char/short/int/long?
    > Is it very important to know them well? Because it's binary,
    > I found that it's a little difficult to understand then decimal.
    >

    They are used quite a lot in everyday C programming.

    For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
    provide a very cheap modulus operation.
    Also, sometimes you need to treat memory as a stream of bits, not a stream
    of bytes. This is typical for compression programs, for example. The only
    way to extract bits is to use the operators to mask out the bits you don't
    want, shift them, and recombine.
    They are also not infrequently used to pack two or more pieces of
    information into a single variable. Fairly commonly you will see functions
    take flags, combined with the OR operator.

    Whilst you can do without them, they are extremely useful, and also one of
    the fundamental operations the processor can perform. Addition is performed
    with a cascade of XOR and other operations, internally, for instance.

    01 + 01 = 10 (binary addition)

    XOR the lowest bit to get the lowest digit of result.
    AND the two bits to get the carry.
    Repeat with carry + one bit in the next place to get a half result
    Repeat with half result and other bit to get the next digit
    OR the two carrys from previous two operations to get the carry.

    Repeat steps until you have all the binary digits, plus a carry.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Sep 13, 2007
    #3
  4. Junmin H.

    Flash Gordon Guest

    Malcolm McLean wrote, On 13/09/07 08:21:
    >
    > "Junmin H." <> wrote in message
    > news:fcag3r$qia$...
    >> Hello, all. I never use them when I am programming.
    >> But i have been reading codes that using them.
    >> When should I use them? Why not use char/short/int/long?
    >> Is it very important to know them well? Because it's binary,
    >> I found that it's a little difficult to understand then decimal.
    >>

    > They are used quite a lot in everyday C programming.
    >
    > For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
    > provide a very cheap modulus operation.


    That is a very BAD reason for using them. Use the modulus operator and
    let the optimiser worry about the optimisation.

    > Also, sometimes you need to treat memory as a stream of bits, not a
    > stream of bytes. This is typical for compression programs, for example.
    > The only way to extract bits is to use the operators to mask out the
    > bits you don't want, shift them, and recombine.
    > They are also not infrequently used to pack two or more pieces of
    > information into a single variable. Fairly commonly you will see
    > functions take flags, combined with the OR operator.


    The above, however, is more reasonable.

    > Whilst you can do without them, they are extremely useful, and also one
    > of the fundamental operations the processor can perform. Addition is
    > performed with a cascade of XOR and other operations, internally, for
    > instance.


    <snip>

    Stating that as absolute truth is incorrect. HW has been done with
    NANDs, although I will admit I don't know current practice. See, for
    example, http://www.ece.uci.edu/docs/hspice/hspice_2001_2-239.html
    The NAND gates might be used to build XOR for some of it, but if the XOR
    is built from NAND gates then XOR is *not* being used as a fundamental
    operation. Once I knew how to NAND gate was constructed in silicon, but
    it is a long time since I did that stuff.
    --
    Flash Gordon
     
    Flash Gordon, Sep 13, 2007
    #4
  5. On Sep 13, 5:07 pm, Flash Gordon <> wrote:
    > Malcolm McLean wrote, On 13/09/07 08:21:
    > > For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
    > > provide a very cheap modulus operation.

    >
    > That is a very BAD reason for using them. Use the modulus operator and
    > let the optimiser worry about the optimisation.


    If, for instance, the value of the variable being used on the right
    side of the modulus operator isn't known at compile-time, it can be a
    very good reason for using bitwise AND. Consider a hash table...
    modulus generally takes many, many more cycles than a bitwise
    operation.
     
    Justin Spahr-Summers, Sep 15, 2007
    #5
  6. Junmin H.

    Flash Gordon Guest

    Justin Spahr-Summers wrote, On 15/09/07 01:17:
    > On Sep 13, 5:07 pm, Flash Gordon <> wrote:
    >> Malcolm McLean wrote, On 13/09/07 08:21:
    >>> For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
    >>> provide a very cheap modulus operation.

    >> That is a very BAD reason for using them. Use the modulus operator and
    >> let the optimiser worry about the optimisation.

    >
    > If, for instance, the value of the variable being used on the right
    > side of the modulus operator isn't known at compile-time, it can be a
    > very good reason for using bitwise AND. Consider a hash table...
    > modulus generally takes many, many more cycles than a bitwise
    > operation.


    There may occasionally be good reasons for doing it. However, most of
    the time I see AND used as a modulus operator it is used with a
    constant, and those cases are bad because they make the code harder to
    read and are more likely to be done incorrectly.
    --
    Flash Gordon
     
    Flash Gordon, Sep 15, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy

    how important are bitwise operators?

    Andy, Nov 10, 2003, in forum: C Programming
    Replies:
    8
    Views:
    704
    Mark A. Odell
    Nov 13, 2003
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    501
    Raymond Hettinger
    Jul 27, 2003
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,603
    Keith Thompson
    Feb 14, 2005
  4. Ioannis Vranos
    Replies:
    8
    Views:
    350
    James Kanze
    Nov 14, 2008
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    258
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page