?bitwise operators

Discussion in 'C++' started by Mike Hodkin, Jan 4, 2004.

  1. Mike Hodkin

    Mike Hodkin Guest

    As a beginning student of C++, books reference "bitwise operators" and give
    brief examples, but I have not read a good explanation of what they are used
    for. One reference mentioned that they are used in hardware programming.

    Are they used very often in routine C/C++ programming, and what for?

    thanks,
    MJH
     
    Mike Hodkin, Jan 4, 2004
    #1
    1. Advertising

  2. Mike Hodkin wrote:
    > As a beginning student of C++, books reference "bitwise operators" and give
    > brief examples, but I have not read a good explanation of what they are used
    > for. One reference mentioned that they are used in hardware programming.
    >
    > Are they used very often in routine C/C++ programming, and what for?


    Yes, they are "often" used.

    Although there are alternatives (bit fields), some code uses bitwise
    operators to manage "flags".

    For example in the Posix open() call where you can set alternative modes
    for openning a file. The "select" system call also uses a "bitmask" to
    select which fd's are interesting.

    They are also used when manipulating data, for example when you want to
    convert from big endian to little endian.

    Certain arithmentic operations are also much less expensive as bit
    operations. For example, if you want a number that is the next largest
    multiple of a power of 2 - ( 1 + ( ( N - 1 ) | ( ( 1 << P ) - 1 ) ).

    It's also used in cryptography, the xor "^" operator is a very useful
    transform since ( ( A^B ) ^B ) == A . This is also used in computation
    of RAID checksums. If you look up "raidzz" it's somthing I wrote a few
    years ago that makes used of bitwise functionality.

    There are plenty more.
     
    Gianni Mariani, Jan 4, 2004
    #2
    1. Advertising

  3. "Mike Hodkin" <> wrote in message
    news:...
    > As a beginning student of C++, books reference "bitwise operators" and

    give
    > brief examples, but I have not read a good explanation of what they are

    used
    > for. One reference mentioned that they are used in hardware programming.
    >
    > Are they used very often in routine C/C++ programming, and what for?
    >


    Not as much as you would think.

    The usually quoted use is for interfaceing to hardware but bitfields not
    portable between different compilers because the standard doesn't mandate
    how they are laid out or accessed (or it didn't - please feel free to
    correct me on this) Consequently it is probably safer to stick to masks and
    bit operators &,|,^

    The only good use is if you really have to use the absolute minimum space
    but even then it may not sit as well with class abstraction as bit masks -
    consider standard i/o - iostate and fmtflags both use masks because they
    have to for the interface and once they are defined for the interface it
    would be foolish to use bitfields in the implementation

    Nick
     
    Nick Hounsome, Jan 4, 2004
    #3
  4. Mike Hodkin

    Howard Guest

    "Nick Hounsome" <> wrote in message
    news:vEYJb.13937$...
    >
    > "Mike Hodkin" <> wrote in message
    > news:...
    > > As a beginning student of C++, books reference "bitwise operators" and

    > give
    > > brief examples, but I have not read a good explanation of what they are

    > used
    > > for. One reference mentioned that they are used in hardware

    programming.
    > >
    > > Are they used very often in routine C/C++ programming, and what for?
    > >

    >
    > Not as much as you would think.
    >


    Maybe not in your work. I use bitwise operators all the time.

    > The usually quoted use is for interfaceing to hardware but bitfields not
    > portable between different compilers because the standard doesn't mandate
    > how they are laid out or accessed (or it didn't - please feel free to
    > correct me on this) Consequently it is probably safer to stick to masks

    and
    > bit operators &,|,^
    >
    > The only good use is if you really have to use the absolute minimum space
    > but even then it may not sit as well with class abstraction as bit masks -
    > consider standard i/o - iostate and fmtflags both use masks because they
    > have to for the interface and once they are defined for the interface it
    > would be foolish to use bitfields in the implementation
    >
    > Nick
    >


    Nick,
    Where did "bitfields" come into this conversation? He asked about
    "bitwise operators", which, to me, means the operators like & and |.
    Nothing to do with bitfields.

    To the original poster:

    the reason bitwise operators are used for hardware is because hardware
    is connected to the computer/processor via sets of wires, usually through
    some kind of I/O port device. The hardware being addressed gets its control
    informaton by which bits in that connecton are on (1) or off (0). And, the
    easiest way to turn a bit on is to OR (|) it with a binary value which has
    only that bit set. Likewise, to set a bit to zero, you would AND (&) it
    with a binary value which has all bits *except* that one set.

    It's also an easy (and compact) way to pass a set of booleans between
    software objects. Instead of sending eight boolean values, you set the bits
    of one unsigned char (for example), and send that in one shot.



    -Howard





    >
     
    Howard, Jan 5, 2004
    #4
  5. "Howard" <> wrote in message
    news:btci6v$...
    >
    > "Nick Hounsome" <> wrote in message
    > news:vEYJb.13937$...
    > >
    > > "Mike Hodkin" <> wrote in message
    > > news:...
    > > > As a beginning student of C++, books reference "bitwise operators" and

    > > give
    > > > brief examples, but I have not read a good explanation of what they

    are
    > > used
    > > > for. One reference mentioned that they are used in hardware

    > programming.
    > > >
    > > > Are they used very often in routine C/C++ programming, and what for?
    > > >

    > >
    > > Not as much as you would think.
    > >

    >
    > Maybe not in your work. I use bitwise operators all the time.
    >
    > > The usually quoted use is for interfaceing to hardware but bitfields not
    > > portable between different compilers because the standard doesn't

    mandate
    > > how they are laid out or accessed (or it didn't - please feel free to
    > > correct me on this) Consequently it is probably safer to stick to masks

    > and
    > > bit operators &,|,^
    > >
    > > The only good use is if you really have to use the absolute minimum

    space
    > > but even then it may not sit as well with class abstraction as bit

    masks -
    > > consider standard i/o - iostate and fmtflags both use masks because they
    > > have to for the interface and once they are defined for the interface it
    > > would be foolish to use bitfields in the implementation
    > >
    > > Nick
    > >

    >
    > Nick,
    > Where did "bitfields" come into this conversation? He asked about
    > "bitwise operators", which, to me, means the operators like & and |.
    > Nothing to do with bitfields.
    >


    sorry - I miss read it.
    Still - I did at least explain why bitwise operators are preferable to
    bitfields
     
    Nick Hounsome, Jan 6, 2004
    #5
    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. Jared Contrascere

    Bitwise Operators

    Jared Contrascere, Jul 19, 2003, in forum: C++
    Replies:
    1
    Views:
    415
    Victor Bazarov
    Jul 19, 2003
  2. Replies:
    3
    Views:
    7,883
    Dave Thompson
    Sep 22, 2003
  3. Replies:
    12
    Views:
    2,803
    Slartibartfast
    Sep 23, 2003
  4. Andy

    how important are bitwise operators?

    Andy, Nov 10, 2003, in forum: C Programming
    Replies:
    8
    Views:
    726
    Mark A. Odell
    Nov 13, 2003
  5. Michael Foord

    Equivalent of Javascript Bitwise Operators

    Michael Foord, Oct 27, 2004, in forum: Python
    Replies:
    2
    Views:
    609
    Bengt Richter
    Oct 28, 2004
Loading...

Share This Page