Bit shift operator


 
Thread Tools Search this Thread
Top Forums Programming Bit shift operator
# 1  
Old 09-04-2006
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
# 2  
Old 09-04-2006
Code:
int main()
{
  int temp=20742;
  short temp1=temp & 0x00ff;
  printf("The value is %d\n",temp1);
  return 0;
}

# 3  
Old 09-05-2006
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
# 4  
Old 09-05-2006
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  
Old 09-08-2006
that's fine Jim... Smilie Smilie Smilie

What's that IMO exactly ..?
# 6  
Old 09-08-2006
IMO: In my opinion
IMHO: In my humble opinion
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

3. Red Hat

boot the 32 bit kernel on a 64 bit PPC Linux machine?

Hi all, I'm looking to cover a corner case for an upcoming test cycle. Is there a way to boot a RedHat Advanced Server 4 (update 3) installed on a Power PC machine to use a 32 bit kernel? This would be similar to what is done here -> https://www.unix.com/aix/26204-aix-platform.html I've done... (0 Replies)
Discussion started by: philrau
0 Replies

4. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question