|
||||||||||||||||||
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 |
|
![]() caldiar Member since: 2/6/2005 From: Aptos, CA, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() jyk GDNet+ Member since: 10/23/2003 |
||||
|
|
||||
Quote: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 ] |
||||
|
||||
![]() Zahlman Moderator - Game Programming Member since: 1/9/2004 From: Toronto, Canada |
||||
|
|
||||
Quote: 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: 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 |
||||
|
||||
![]() caldiar Member since: 2/6/2005 From: Aptos, CA, United States |
||||
|
|
||||
| Interesting and informative stuff as always guys. Thanks a ton for the explanation :) |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|