Thread: Is This Site's Tutorial on Bitwise Operation Correct?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    161

    Is This Site's Tutorial on Bitwise Operation Correct?

    I'm reading Alex's tutorial on Bitwise operation on this website.
    I'm a bit confused (no pun intended) with this following description which seems incorrect:

    ***

    The leftshift operator is the equivalent of moving all the bits of a number a specified number of places to the left:[variable]<<[number of places]
    For instance, consider the number 8 written in binary 00001000. If we wanted to shift it to the left 2 places, we'd end up with 00100000; everything is moved to the left two places, and zeros are added as padding. This is the number 32 -- in fact, left shifting is the equivalent of multiplying by a power of two.

    Code:
    int mult_by_pow_2(int number, int power)
    {
        return number<<power;
    }
    ***
    Specifically the line in red.
    It appears to me this is incorrect; that the leftshift operator is the equivalent of multiplying by two (not by the power of two).
    So 8<<1 is 8*2 or 16. 8<<2 is 8*2*2 or 32. Where as 8^2 (8 to the power of 2) is actually 64.

    Am I somehow mis-interpreting this?
    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You are misinterpreting it. Consider a number in binary.
    Code:
    2^ 8 7 6 5 4 3 2 1 0 
       0 0 0 0 0 1 0 0 0
       
       n = 8<<2;
       
    2^ 8 7 6 5 4 3 2 1 0 
       0 0 0 1 0 0 0 0 0
    Hopefully with the place values you can see how left shifting increases the value the same way you would multiply by powers of 2.
    Last edited by whiteflags; 08-24-2014 at 06:36 PM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Within limits, m << n is the equivalent to m * 2^n (where 2^n means 2 raised to the power n).

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    I see. Maybe I would have understood it better if it had said "left shifting is equivalent to multiplying by an additional power of 2."

    Thanks

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by FloridaJo View Post
    I see. Maybe I would have understood it better if it had said "left shifting is equivalent to multiplying by an additional power of 2."

    Thanks
    Weird but true, left shifting in any base is equivalent to multiplying by a power of whatever the base is. Going to the right is the equivalent of division by that number.

    eg. base 10 : 156 * 10 = 1560;

    Same thing in base 12 : 110 * 10 = 1100 ( same as 156 * base = left shift ).

    Hope that is helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2013, 08:50 AM
  2. C Bitwise Operation..
    By ahmedBanihammad in forum C Programming
    Replies: 4
    Last Post: 10-31-2010, 09:11 AM
  3. Bitwise operation; flag look up
    By emptythought in forum C Programming
    Replies: 22
    Last Post: 09-08-2010, 06:48 AM
  4. Bitwise operation problem
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2008, 01:49 PM
  5. Bitwise operation
    By Justice in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 09:06 PM

Tags for this Thread