Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 5th, 2010, 04:11 PM
cpeava cpeava is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 cpeava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m 44 sec
Reputation Power: 0
Bit Shifting

In computer architecture, why do we shift bits? More specifically, why do shift the bits in registers?

Is it so we can store multiple numbers for different purposes in the same register, and then just shift the bits around to get/put a number?

Reply With Quote
  #2  
Old October 5th, 2010, 05:50 PM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,056 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 5 h 36 m 30 sec
Reputation Power: 7104
Quote:
In computer architecture, why do we shift bits? More specifically, why do shift the bits in registers?

Because shifting bits is fast as hell, especially when the bits are (already) stored in a register.

Left shifting is equivalent to multiplying by 2, that's the most prominent arithmetic use of bit shifting.

It's also a very fast way of extracting a portion of a word. Wrapping multiple pieces of data into a single word is more efficient both space-wise and speed-wise if the data is less than one word. Space-wise it's more efficient because you don't have to waste the extra storage space for the unused bits of the word, and speed-wise because you can load multiple pieces of data using a single load instruction rather than having to issue a load instruction for each piece of data.

Unless you're programming in assembly I don't recommend storing multiple pieces of data in a single variable though. Even if you're programming in assembly, I don't recommend doing it unless you have to.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old October 5th, 2010, 09:19 PM
NovaX NovaX is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2005
Location: Bay Area, California
Posts: 841 NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level)NovaX User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 12 h 59 m 16 sec
Reputation Power: 1680
Send a message via ICQ to NovaX Send a message via Yahoo to NovaX Send a message via Google Talk to NovaX
Its fast. Really, really, really fast. By understanding how to use binary effectively you can solve problems extremely quickly. Lets take a few examples.

First, a recent Java forum question was how to calculate the power of two greater than the given number. The general advise was to use either an O(n) loop to calculate all powers until found, or to use log2 arithmetic. A simpler and fast answer is to take advantage of the binary representation. After finding the highest 1 bit, a left shift's property of being a multiple of two was used to find the next power.

Another simple example is to generate a powerset. An easy implementation is to use a bit per element and an integer as a counter. For each permutation, increment the counter and evaluate each bit field to determine membership in the output set. The shift operator is used to create the bit mask to zero out all of the other bit fields.

This use as a bit mask has a lot of power. For example, a bloom filter is a very compact data structure that provides a probabilistic Set. It uses a bit-vector and multiple hashing functions to set bit fields. Membership is determined by seeing if an element's bit indices are set. This allows not storing the actual element itself at the cost of false positives. It is therefore a "filter" because it allows us to remove the negative cases, thereby reducing how often we perform an expensive operation (e.g. I/O call).
__________________
Core design principles when developing software systems.
See my open-source project as an example of professional code.
---
The opinions expressed do not represent those of my employer.

Reply With Quote
  #4  
Old October 11th, 2010, 01:30 AM
cpeava cpeava is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 cpeava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 35 m 44 sec
Reputation Power: 0
Thank you for the explanations.

Reply With Quote
  #5  
Old November 9th, 2010, 11:51 PM
localtraffic localtraffic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Location: Southampton PA
Posts: 14 localtraffic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 50 m 45 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Bit Shifting

Bit shifting basically use sign representation and to store large value in small size register.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Bit Shifting

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap