The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shift and push question in perl hankooknara Shell Programming and Scripting 5 06-29-2007 03:37 AM
Regarding the shift command??? shrao Shell Programming and Scripting 2 03-31-2007 12:39 AM
shift command Nisha Shell Programming and Scripting 6 07-19-2002 02:54 AM
xterm SHIFT crazy oneivan UNIX for Dummies Questions & Answers 2 06-05-2002 01:29 AM
shift command AkumaTay UNIX for Dummies Questions & Answers 1 05-20-2002 05:26 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-03-2006
Registered User
 

Join Date: Jul 2006
Posts: 24
Bit shift operator

Can someone pls tell me how is the operation different in the following two code snippets?


main()
{
int temp=20742;

short temp1;
temp1 = temp << 8;
printf("The vaue is %d\n",temp1>>8);}

result:6

main()
{
int temp=20742;

short temp1;
temp1 =(temp << 8)>>8;
printf("The vaue is %d\n",temp1);}

result: 20742

If I want to do the operation that I have done in the first code snippet in a single line without using any temp variables, how do I do it?

Thanks,
Anitha
Reply With Quote
Forum Sponsor
  #2  
Old 09-04-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
Code:
int main()
{
  int temp=20742;
  short temp1=temp & 0x00ff;
  printf("The value is %d\n",temp1);
  return 0;
}
Reply With Quote
  #3  
Old 09-04-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by naan
Can someone pls tell me how is the operation different in the following two code snippets?


main()
{
int temp=20742;

short temp1;
temp1 = temp << 8;
printf("The vaue is %d\n",temp1>>8);}

result:6

main()
{
int temp=20742;

short temp1;
temp1 =(temp << 8)>>8;
printf("The vaue is %d\n",temp1);}

result: 20742

If I want to do the operation that I have done in the first code snippet in a single line without using any temp variables, how do I do it?

Thanks,
Anitha
you have used short datatype for temp1,
sizeof short datatype=2
sizeof int datatype=4

use as int temp1
Reply With Quote
  #4  
Old 09-05-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
Matrix -
IMO - the OP is trying to zero out the three high-order bytes in the temp variable.
You are right to keep datatypes matching and the OP should just "and" away unwanted stuff:
Code:
int temp=20742 & 0x000000ff;
Reply With Quote
  #5  
Old 09-07-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
that's fine Jim...

What's that IMO exactly ..?
Reply With Quote
  #6  
Old 09-07-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
IMO: In my opinion
IMHO: In my humble opinion
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0