Thread: Left and right shift operators

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    15

    Left and right shift operators

    Hi all,

    I understand that the left and right shift (<<, >>) operators shift bits around but can someone give me an example of when we would want to do this? I've read that left shift is equivalent to multiplication by two but if shifting a number ends up evaluating to another number, why not just hardcode that other number instead of shifting and having to calculate out what the shift is doing?

    For example, in GObject we have things like:

    Code:
    #define	G_TYPE_MAKE_FUNDAMENTAL(x)	((GType) ((x) << G_TYPE_FUNDAMENTAL_SHIFT))
    where

    Code:
    #define	G_TYPE_FUNDAMENTAL_SHIFT	(2)
    It appears the value to G_TYPE_MAKE_FUNDAMENTAL is therefore shifted two bits left. What's the point?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eclipse07
    I understand that the left and right shift (<<, >>) operators shift bits around but can someone give me an example of when we would want to do this? I've read that left shift is equivalent to multiplication by two but if shifting a number ends up evaluating to another number, why not just hardcode that other number instead of shifting and having to calculate out what the shift is doing?
    Because hardcoding a power of two can obscure bitwise manipulation, and can also be less flexible in that you actually have to precompute the number rather than let the compiler do it for you. That is, you would shift bits rather than multiply or divide by a power of two because you want to express something about the bits rather than because you just coincidentally want to multiply or divide by a power of two.

    An example would be if you're trying to set/test bits of a number in a loop: it would be easier to loop from 0 to N rather than to hardcode all those values corresponding to the bit positions.

    Quote Originally Posted by Eclipse07
    It appears the value to G_TYPE_MAKE_FUNDAMENTAL is therefore shifted two bits left. What's the point?
    It could be that the author wants to express the notion that the first two bits are reserved to designate something else, e.g., something reserved to the library implementation.
    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

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    15
    Ok, well thanks for the reply.

    I think you're probably right about the library implementation using the rightmost bits for some specific purpose, maybe to indicate some aspect of the type. Hadn't thought of that specifically. I just saw the numbers 0 through 20, and thought what's the point here.

    Code:
    /**
     * G_TYPE_INVALID:
     * 
     * An invalid #GType used as error return value in some functions which return
     * a #GType. 
     */
    #define G_TYPE_INVALID			G_TYPE_MAKE_FUNDAMENTAL (0)
    /**
     * G_TYPE_NONE:
     * 
     * A fundamental type which is used as a replacement for the C
     * void return type.
     */
    #define G_TYPE_NONE			G_TYPE_MAKE_FUNDAMENTAL (1)
    /**
     * G_TYPE_INTERFACE:
     * 
     * The fundamental type from which all interfaces are derived.
     */
    #define G_TYPE_INTERFACE		G_TYPE_MAKE_FUNDAMENTAL (2)
    /**
     * G_TYPE_CHAR:
     * 
     * The fundamental type corresponding to #gchar.
     * The type designated by G_TYPE_CHAR is unconditionally an 8-bit signed integer.
     * This may or may not be the same type a the C type "gchar".
     */
    #define G_TYPE_CHAR			G_TYPE_MAKE_FUNDAMENTAL (3)
    /**
     * G_TYPE_UCHAR:
     * 
     * The fundamental type corresponding to #guchar.
     */
    #define G_TYPE_UCHAR			G_TYPE_MAKE_FUNDAMENTAL (4)
    /**
     * G_TYPE_BOOLEAN:
     * 
     * The fundamental type corresponding to #gboolean.
     */
    #define G_TYPE_BOOLEAN			G_TYPE_MAKE_FUNDAMENTAL (5)
    /**
     * G_TYPE_INT:
     * 
     * The fundamental type corresponding to #gint.
     */
    #define G_TYPE_INT			G_TYPE_MAKE_FUNDAMENTAL (6)
    /**
     * G_TYPE_UINT:
     * 
     * The fundamental type corresponding to #guint.
     */
    #define G_TYPE_UINT			G_TYPE_MAKE_FUNDAMENTAL (7)
    /**
     * G_TYPE_LONG:
     * 
     * The fundamental type corresponding to #glong.
     */
    #define G_TYPE_LONG			G_TYPE_MAKE_FUNDAMENTAL (8)
    /**
     * G_TYPE_ULONG:
     * 
     * The fundamental type corresponding to #gulong.
     */
    #define G_TYPE_ULONG			G_TYPE_MAKE_FUNDAMENTAL (9)
    /**
     * G_TYPE_INT64:
     * 
     * The fundamental type corresponding to #gint64.
     */
    #define G_TYPE_INT64			G_TYPE_MAKE_FUNDAMENTAL (10)
    /**
     * G_TYPE_UINT64:
     * 
     * The fundamental type corresponding to #guint64.
     */
    #define G_TYPE_UINT64			G_TYPE_MAKE_FUNDAMENTAL (11)
    /**
     * G_TYPE_ENUM:
     * 
     * The fundamental type from which all enumeration types are derived.
     */
    #define G_TYPE_ENUM			G_TYPE_MAKE_FUNDAMENTAL (12)
    /**
     * G_TYPE_FLAGS:
     * 
     * The fundamental type from which all flags types are derived.
     */
    #define G_TYPE_FLAGS			G_TYPE_MAKE_FUNDAMENTAL (13)
    /**
     * G_TYPE_FLOAT:
     * 
     * The fundamental type corresponding to #gfloat.
     */
    #define G_TYPE_FLOAT			G_TYPE_MAKE_FUNDAMENTAL (14)
    /**
     * G_TYPE_DOUBLE:
     * 
     * The fundamental type corresponding to #gdouble.
     */
    #define G_TYPE_DOUBLE			G_TYPE_MAKE_FUNDAMENTAL (15)
    /**
     * G_TYPE_STRING:
     * 
     * The fundamental type corresponding to nul-terminated C strings.
     */
    #define G_TYPE_STRING			G_TYPE_MAKE_FUNDAMENTAL (16)
    /**
     * G_TYPE_POINTER:
     * 
     * The fundamental type corresponding to #gpointer.
     */
    #define G_TYPE_POINTER			G_TYPE_MAKE_FUNDAMENTAL (17)
    /**
     * G_TYPE_BOXED:
     * 
     * The fundamental type from which all boxed types are derived.
     */
    #define G_TYPE_BOXED			G_TYPE_MAKE_FUNDAMENTAL (18)
    /**
     * G_TYPE_PARAM:
     * 
     * The fundamental type from which all #GParamSpec types are derived.
     */
    #define G_TYPE_PARAM			G_TYPE_MAKE_FUNDAMENTAL (19)
    /**
     * G_TYPE_OBJECT:
     * 
     * The fundamental type for #GObject.
     */
    #define G_TYPE_OBJECT			G_TYPE_MAKE_FUNDAMENTAL (20)
    Last edited by Eclipse07; 12-15-2019 at 08:13 PM.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Last edited by Hodor; 12-15-2019 at 08:23 PM.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Ugh, I can't edit my previous message without it all ending up being a link. Anyway, the above link is where/when those changes were made. You can read the how thread starting from gtype.h broken for 64-bit

    The first 2 bits are, as laserlight guessed, essentially reserved[1]; they're for G_TYPE_INVALID and G_TYPE_NONE

    Things seem to have become slightly obfuscated by fixing the 64-bit bug (it was more apparent what the left shift was there for in the original code; hopefully you know how to interpret diffs)

    Edit: [1] Probably because programs are not meant to change those bits, only the library (but that's just a hunch, I'm not familiar with the code)
    Last edited by Hodor; 12-15-2019 at 08:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c bit shift left and right
    By ssm in forum C Programming
    Replies: 8
    Last Post: 03-01-2016, 02:09 PM
  2. Shift Left, Shift Right Question
    By congi in forum C Programming
    Replies: 1
    Last Post: 01-28-2015, 10:45 AM
  3. Left shift question
    By patishi in forum C Programming
    Replies: 8
    Last Post: 09-17-2013, 06:29 PM
  4. bit (or) operation,left shift
    By Hassan Ahmed in forum C Programming
    Replies: 5
    Last Post: 07-19-2013, 02:15 PM
  5. Left Shift
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 11:15 AM

Tags for this Thread