Thread: Bitwise Operator Shift

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Angry Bitwise Operator Shift

    Hey everyone,

    I'm having a problem understanding bitwise operators.
    I have an exercise where x is given (x=1340) and i need to calculate x<<2 which is 5360

    first of all i know how to convert into a binary form so
    1340 = 10100111100 and applying <<2 = 10011110000 (right?)

    now that I have that how to i convert it for it to be 5360?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1340 = 10100111100 and applying <<2 = 10011110000 (right?)
    Not exactly - you chopped off the two highest bits:

    Code:
    10100111100
    1010011110000
    Are you supposed to be converting to binary, shifting, then converting back to decimal?

    Because otherwise, if you just shift the [decimal] value in code, the binary stuff takes place "behind the scenes".

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Not exactly - you chopped off the two highest bits:

    Code:
    10100111100
    1010011110000
    Are you supposed to be converting to binary, shifting, then converting back to decimal?

    Because otherwise, if you just shift the [decimal] value in code, the binary stuff takes place "behind the scenes".
    Well given x when we code it so
    Code:
    int main()
    {
    int x = 1340;
    printf("%d\n", x<<2);
    }
    could you explain what i chipped off?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    By "chopped off", I was referring to how you manually wrote the binary (which I quoted in my post).

    So ... you implemented what I explained. Does that answer your question?

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Not quite, i'm looking to know how to manually get the output without coding it

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by YannB View Post
    Not quite, i'm looking to know how to manually get the output without coding it
    Do you understand what an arithmetic left shift does? I mean given an unsigned number, say 5, you know the answer to 5 << 1? I apologise if this sounds like a simple question, I'm just making sure.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The least significant bit (the right most, bit zero) has a "weight" of 20.
    The next one, going left (bit one) has a "weight" of 21.
    The next one, going left (bit two) has a "weight" of 22.
    This continues on.

    If there is a "one" in a position, you add the corresponding "weight" to the total. If a "zero", you don't.

    So, for instance, let's look at 1001.

    That would be: 23 + 22 + 21 + 20

    Since the middle two are zeros, we can ignore them.

    23 + 0 + 0 + 20

    23 = 8
    20 = 1

    So 1001 is equal to 8 + 1 = 9.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    don't worry about it appreciate your help.
    i know that it's a bitwise operation which means that it should be shifting left side right?

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    The least significant bit (the right most, bit zero) has a "weight" of 20.
    The next one, going left (bit one) has a "weight" of 21.
    The next one, going left (bit two) has a "weight" of 22.
    This continues on.

    If there is a "one" in a position, you add the corresponding "weight" to the total. If a "zero", you don't.

    So, for instance, let's look at 1001.

    That would be: 23 + 22 + 21 + 20

    Since the middle two are zeros, we can ignore them.

    23 + 0 + 0 + 20

    23 = 8
    20 = 1

    So 1001 is equal to 8 + 1 = 9.
    That i completely understand without a doubt.
    my problem is calculating the code i showed all of you by hand

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Ahh, now I think I see. Remember when I originally said the highest two bits were "chopped off"?

    If you assume there are a few more bits than are shown, then you shouldn't "lose" any of the upper bits.

    You wrote 1340 as:

    10100111100

    You could also think of it as:

    0010100111100

    Note I only added two zeroes at the front (which would not change the value).

    Now shift it left twice:

    0010100111100 (original)
    0101001111000 (shifted left once)
    1010011110000 (shifted left again)

    And there you go!

    If you play scrabble, you can visualize it like so. Image you use two tiles. Then you grab two fresh ones from the bag and stick them, one at a time, on the right side of you letter holder. All the tiles already in the letter holder will "shift leftwards".

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Ahh, now I think I see. Remember when I originally said the highest two bits were "chopped off"?

    If you assume there are a few more bits than are shown, then you shouldn't "lose" any of the upper bits.

    You wrote 1340 as:

    10100111100

    You could also think of it as:

    0010100111100

    Note I only added two zeroes at the front (which would not change the value).

    Now shift it left twice:

    0010100111100 (original)
    0101001111000 (shifted left once)
    1010011110000 (shifted left again)

    And there you go!

    If you play scrabble, you can visualize it like so. Image you use two tiles. Then you grab two fresh ones from the bag and stick them, one at a time, on the right side of you letter holder. All the tiles already in the letter holder will "shift leftwards".
    okay true that i get again the problem is getting the output of the code in hand?

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    the output of the code is 5360 how do i get to that?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    By applying the information I provided in post #7 (which you subsequently said you understood without a doubt).

    Quote Originally Posted by Matticus, post #10
    Now shift it left twice:

    0010100111100 (original)
    0101001111000 (shifted left once)
    1010011110000 (shifted left again)
    So the final result is: 1010011110000

    That is:

    212 + 0 + 210 + 0 + 0 + 27 + 26 + 25 + 24 + 0 + 0 + 0 + 0 =

    4096 + 1024 + 128 + 64 + 32 + 16 =

    5360

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    By applying the information I provided in post #7 (which you subsequently said you understood without a doubt).



    So the final result is: 1010011110000

    That is:

    212 + 0 + 210 + 0 + 0 + 27 + 26 + 25 + 24 + 0 + 0 + 0 + 0 =

    4096 + 1024 + 128 + 64 + 32 + 16 =

    5360
    thanks that's exactly what i needed. I have a question though,
    why did you add 2 zeros (i know that it doesnt change the value) but there has to be a reason why? and do i need to do this for any decimal number?

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Those zeros were added for illustrative purposes. We could have just as well said:

    Code:
    /* ... */
      10100111100 (original)
     101001111000 (shifted left once)
    1010011110000 (shifted left again)
    I showed those zeros to you because, in your original post, it seems were you trying to preserve the number of bits in the original value. The idea is that there are other bit places beyond the currently set "most significant bit". We just don't see them because they're irrelevant - they have no value, so we could just think of them as zero (empty place holders).

    This is what you implied in your original post:

    1340 = 10100111100 and applying <<2 = 10011110000 (right?)
    Code:
    /* wrong! */
      10100111100 (original)
      01001111000 (shifted left once)
      10011110000 (shifted left again)
    Notice how in your original statement, the starting 1 and 0 get lost during shifting. It seems like you were trying to preserve the number of bits, and in doing so were getting the wrong result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help bitwise shift
    By progmateur in forum C Programming
    Replies: 1
    Last Post: 03-30-2013, 03:00 PM
  2. Cycle shift with bitwise operators, help pls
    By kevinstrijbos in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 12:15 PM
  3. Bitwise Shift
    By blurx in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 09:39 AM
  4. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 01:18 PM
  5. Bitwise Shift Operations
    By John_L in forum Tech Board
    Replies: 19
    Last Post: 02-25-2008, 11:22 AM