Thread: Left Shift

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Left Shift

    Which one is equivalent to multiplying by 2:

    Left shifting a no. by 1 or Left shifting an unsigned int or char by 1 ?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    both? while they don't cause numeric overflow of unsigned int and unsigned char correspondingly
    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

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Both are while they don't overflow, but if the number is signed then the shift could make it negative.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    thanx

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by mike_g View Post
    Both are while they don't overflow, but if the number is signed then the shift could make it negative.
    If you do an arithmetic left shift on a signed number, that can never happen. If you do a logical shift, it can. A C compiler determines which one to do based upon whether the number to shift is signed or unsigned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. utility of left and right shift operator
    By bhagwat_maimt in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2006, 10:36 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM