Thread: Shift Left, Shift Right Question

  1. #1
    Registered User
    Join Date
    Jan 2015

    Shift Left, Shift Right Question

    I was learning about shifting and can't help but think, what's the point? Why shift left or right a number by 1 or 2 rather than just reassign the number completely?

    I guess I'm looking for an explanation, esoteric origins or scenarios where this is useful.

    Thank you,


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The alternative to shifting would be to multiply or divide by a power of 2. However, when one is dealing with bits, it may be conceptually easier to do bit shifting instead of multiplying or dividing by a power of 2 (even if the end result after optimisations applied by the compiler is the same). As for why one might deal with bits: perhaps one wants to store boolean values in a bitset.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left shift question
    By patishi in forum C Programming
    Replies: 8
    Last Post: 09-17-2013, 06:29 PM
  2. Left shift a string
    By Tigers! in forum C Programming
    Replies: 10
    Last Post: 08-16-2009, 11:58 PM
  3. Left Shift
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 11:15 AM
  4. left shift operator!!!!
    By theblackhat in forum C Programming
    Replies: 2
    Last Post: 10-02-2004, 02:07 AM
  5. Left Shift problem
    By Mox in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2001, 03:58 AM