![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
int main()
{
int temp=20742;
short temp1=temp & 0x00ff;
printf("The value is %d\n",temp1);
return 0;
}
|
|
#3
|
|||
|
|||
|
Quote:
sizeof short datatype=2 sizeof int datatype=4 use as int temp1 |
|
#4
|
|||
|
|||
|
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; |
|
#5
|
|||
|
|||
|
that's fine Jim...
What's that IMO exactly ..? |
|
#6
|
||||
|
||||
|
IMO: In my opinion
IMHO: In my humble opinion |
||||
| Google The UNIX and Linux Forums |