Thread: Bitwise Operators

  1. #1
    Registered User (^Burt^)'s Avatar
    Join Date
    Sep 2013
    Posts
    26

    Bitwise Operators

    Could someone please give me an example of when and why we would use bitwise operators. I'm trying to build a detailed understanding as to what and why we use such things.

    One example I know of is the << left shift being used for the power of.
    What else would it be used for?

    Looking at the & I can predict and produce the expected results as per the tutorial but why would we need to do such an operation.

    Many thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Here's an example, from a very recent post: How to split an int, into 2-Bytes???? Controlling PWM with an int.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's also this thread.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Most file formats formats will describe how integer values are stored in the bytes 0,1,2,3

    Which maybe different from actual byte order of the integer value on the current platform.
    So to write portable code you
    1. Read int as whole and then call something like ntos which will swap bytes if platform requires it.
    2. Or read bytes as array and then assemble integer from it using bit shifting and ORing

    The stream formats go further - describing fields as bit position Like CMR field takes 4 low bits of first byte of AMR payload of each RTP packet...
    In this case to extract the field value you HAVE to use the bit shifting and masking

    Same goes for opposite direction operations - when you want to store the file of specific format in portable way or generate outgoing stream according to its format requirements
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well, there's all these sorts of things:
    Bit Twiddling Hacks
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators help ~
    By Siaw Ys in forum C Programming
    Replies: 7
    Last Post: 12-11-2011, 02:02 AM
  2. bitwise operators
    By manzoor in forum C++ Programming
    Replies: 12
    Last Post: 08-18-2008, 02:43 PM
  3. bitwise operators??!?
    By Mr_Jack in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2004, 08:05 AM
  4. Bitwise operators
    By Unregistered in forum C Programming
    Replies: 22
    Last Post: 03-24-2004, 11:03 AM
  5. Bitwise operators
    By kerrywolfe in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2002, 05:57 PM