Home » Community » Forums » For Beginners » [C] Assigning Bit Shifts to Int
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 [C] Assigning Bit Shifts to Int
Post New Topic  Post Reply 
I just happened to be crawling through some C source and something caught my eye.

An int variable was being declared as "int variable_name = (1 << 10)" instead of "int variable_name = 1024"

Is there a reason things were done with a bit shift? From what I could find on Google it's been suggested that this is maybe an optimization trick. Can anybody confirm/debunk this and give an explination as to why a bit shift is being used rather than just using its result directly?

Thanks.

 User Rating: 1180   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
I just happened to be crawling through some C source and something caught my eye.

An int variable was being declared as "int variable_name = (1 << 10)" instead of "int variable_name = 1024"

Is there a reason things were done with a bit shift? From what I could find on Google it's been suggested that this is maybe an optimization trick. Can anybody confirm/debunk this and give an explination as to why a bit shift is being used rather than just using its result directly?

Thanks.
Usually this is just done for clarity and documentation purposes (in my experience, at least). Using the 'shift' notation helps document that the value is intended to be used as a flag that will be stored in a single bit, and also indicates the index of the bit that will be used to store the flag.

It can also make it easier to generate the flag values. Even though enumerating the first 10 or 20 powers of two is easy for most programmers, enumerating consecutive integers is easier still. Sometimes you'll also see the expression wrapped in a macro or function, so that you can write e.g.:
enum {
    APPLE  = BIT(0),
    ORANGE = BIT(1),
    BANANA = BIT(2),
    ...
};



[ Configurable Math Library ]

 User Rating: 1986   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

Quote:
Original post by caldiar
Is there a reason things were done with a bit shift?


Documentation, as jyk said.

1024 means the number one thousand, twenty four.
1 << 10 means a value which has bit 10 set and all other bits clear.

Of course, these are the same value, but the expressions emphasize different things about that value. It's kind of like how one sometimes uses a big word and other times decides that a diminuitive one will do. :)

Quote:
From what I could find on Google it's been suggested that this is maybe an optimization trick. Can anybody confirm/debunk this


Debunked: for such a simple case, today's compilers are much, much too smart for something like this to make a difference.

On some hardware, asking the CPU to shift may be faster than asking it to multiply. Of course, shifting only accomplishes multiplication by powers of 2. In the olden days, the speed difference could be quite significant, so that even multiplication by other numbers could be made faster by shifting and adding, for sufficiently "simple" numbers. For example, x * 320 = x * 256 + x * 64, so people would write the code to do two shifts (one by 8 and the other by 6) and add the results.

But that only works when at least one of the terms in the multiplication is known ahead of time.

And none of that matters today, because
(a) if it were faster, the compiler would know about it, and automatically translate the multiplication into shift and add logic, because we know how to do these kinds of things now (and don't mind putting in the extra effort for compiler optimizations). There is nothing remotely resembling a direct mapping between your source code statements and the final machine code.

(b) when both arguments for a multiplication (or a shift, for that matter - such as 1 << 10) are known ahead of time, the compiler just does the multiplication and stores the result directly in the program. Of course, when you just give the answer (1024), that gets stored directly as well.

(c) on modern hardware, it isn't actually clear that it would help. I've been told for example that current Pentiums lack the circuit that would normally be used to shift by multiple bits at once. And the circuits that perform multiplication have some interesting optimizations at the hardware level, too. (The overall processor design is also much more complex than it used to be, such that it doesn't really make sense to talk about it taking X clock cycles for a shift or Y cycles for a multiplication.)



As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.
"OMG! I'm so happy! I have "1 Friends"!!!" -- coldacid
"Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety." -- SiCrane
"I mean, if you had sex for every time O'Reilly used the word Patriotism you'd be almost as awesome as Chuck Norris." -- tthibault

<triforce101> uh im not a noob i finished the game

 User Rating: 1992   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Interesting and informative stuff as always guys.

Thanks a ton for the explanation :)

 User Rating: 1180   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: