The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. January 07, 2009, 11:55:02 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Quick multiplication by 2  (Read 589 times)
m0nkeymafia
Expert
****
Offline Offline

Posts: 236


Check it!


View Profile
« on: May 28, 2007, 09:11:03 AM »

Im bored so thought id post again in my C++ board Tongue

Its a simple but pretty cool technique called bit shifting.
It allows you to very quickly multiply or divide integers by 2.

The theory:
If you dont know already integers are stored [normally] as 4 bytes within the computer, meaning you have 32 bits to play with.  On PCs they are stored in a certain way [little endian].  Basically an integer, stored as binary, may look like this:

The number 2
000000000000000000000000000010 [31 zeroes and a 2, and yes i cant count Tongue]
The number 4 is exactly the same as 2, but shifted along one column
000000000000000000000000000100
The number 8 follows the same pattern
000000000000000000000000001000

So we can see we can mutiply [and indeed divide] by 2 by moving the bits in an integer up and down.  This is called shifting.

So this is how we can code using it:
int myNum = 2;
//Times by 8
myNum = myNum << 3; //Note 8 is 2^3

But why would you use this?
Most modern compilers will actually use this method [on compile time] if it can guaruntee you are multiplying by 2,

i.e
myNum = myNum * 2;

BUT, if you multiply by another variable x, it doesnt know it will always be a 2.
i.e.
myNum = myNum * x;

But if you know x is always a 2 you can shave off a few cycles by using bit shifting.
This really comes into its own when you are writing complex time critical code and need to shave off as much execution time as possible.  Plus its really cool and you can pull women with it Tongue
Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5324


:sniffle: Humor was so much easier before.


View Profile
« Reply #1 on: May 28, 2007, 08:29:22 PM »

But if you know x is always a 2 you can shave off a few cycles by using bit shifting.
This really comes into its own when you are writing complex time critical code and need to shave off as much execution time as possible.  Plus its really cool and you can pull women with it Tongue

Actually can be way more than a few cycles in a complex app... I've run across more than a few applications where the code was actually factored towards 2x2 mult/div everywhere possible. I doubt that the effect is as huge in a scripted language like PHP, but you're right, in a compiled language it's a great technique.
Logged

If I can't be Mr. Root then I don't want to play.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!