LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2003, 10:03 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
Shift operator


I have a troube in the program from <<thinking in c++>>,
Code:
int main ()
{
   unsigned int getval;
   cout<<"enter a number between 0 and 255:";
   cin>>getval;
   unsigned char a=getval;
   cout<<getval<<" in binary is: ";
   for(int i = 7; i>=0; i--)
      if(a&(1<<i))
         cout<<"1";
      else
         cout<<"0";
   cout<<endl;
}
the author said: "The << and >> provide bit-shifting behavior, but when they shift bits off the end of the number, those bits are lost (it¡¯s commonly said that they fall into the mythical bit bucket, a place where discarded bits end up, presumably so they can be reused...)."
I don't know what are the losted bits.
Could someone give a example about the bits losted to me?
Thank you.

Last edited by Xiangbuilder; 10-03-2003 at 10:08 PM.
 
Old 10-03-2003, 10:50 PM   #2
QtCoder
Member
 
Registered: Aug 2003
Location: USA
Distribution: Slackware 12.0 RC1
Posts: 129

Rep: Reputation: 15
Hi.

To truly understand where bits go when doing bitwise shifting, you should take a look at an assembly programming tutorial.

Although I can't remember all about assembly, I do remember that when you shift bits, the last bit to be shifted off ends up in the "carry register", a specific location in CPU. The bit that's shifted into the carry register can be used to perform complex arithmetic involving carrying a digit.

That's the extent of my knowledge. You'd be amazed how much you learn about C++ when you study assembly. If you truly like to know how things work, do a google for "assembly tutorial." I finally understand how dynamic memory allocation works, how arguments are actually passed to functions, how a return statement works! :-)

Last edited by QtCoder; 10-04-2003 at 12:22 AM.
 
Old 10-03-2003, 11:34 PM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Take this byte into consideration.

Code:
00100111
Let's say we did a left shift of 2 bits on this byte.
All bits shift to the left two spaces, creating the new
byte:

Code:
10011100
Remember the two zeros on the left of the byte before we shifted? They are now gone, lost forever, which is what we
wanted. The same is true when you do a right shift. Bits on the
right of the byte will be lost.

When the author of your book said:

Code:
it ¡s commonly said that they fall into the 
mythical bit bucket, a place where discarded bits 
end up, presumably so they can be reused...
He was trying to make a joke, I hope. Because what he said
is a total lie, and 100% false.
 
Old 10-04-2003, 01:17 AM   #4
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I think I understand it now by your help.
Here is a program to show the result:
Code:
#include<iostream>
using namespace std;
void func (unsigned char a);
int main ()
{
   unsigned int getval;
   cout<<"enter a number between 0 and 255:";
   cin>>getval;
   unsigned char a=getval;
   cout<<getval<<" in binary is: ";
   func (a);
   a>>=2;
   cout<<"("<<getval<<">>2) in binary is: ";
   func (a);
} 
void func (unsigned char a) {
   for(int i=7; i>=0; i--)
      if(a&(1<<i))
         cout<<"1";
      else
         cout<<"0"; 
   cout<<endl; 
}
# ./rotation_a
enter a number between 0 and 255:39
39 in binary is: 00100111
(39>>2) in binary is: 00001001
#
 
Old 10-04-2003, 02:07 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Looks like it works
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I shift my screen over? bad_andy Mandriva 6 10-24-2004 10:52 AM
shift operator linuxanswer Programming 1 05-01-2004 07:33 PM
Shift the Desktop... LordGoddard Linux - Software 15 04-28-2004 03:26 PM
~ operator linuxanswer Programming 7 04-08-2004 04:56 PM
MS strategy shift awdoyle General 13 12-11-2002 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration