typecasting int to long long int 
Author Message
 typecasting int to long long int

Hi,

I am having a integer variable.

int id;

what will happen when i typecast this 'id' to a 64bit type i.e.

((long long int )id << 32 ).

What would be the result of this expression( assume id = 3 ). How this
expression is manipulated. Kindly explain me in detail.

TIA,
Bryan



Wed, 21 Jul 2004 15:44:15 GMT  
 typecasting int to long long int

    B> Hi,
    B> I am having a integer variable.

    B> int id;

    B> what will happen when i typecast this 'id' to a 64bit type i.e.

    B> ((long long int )id << 32 ).

    B> What would be the result of this expression( assume id = 3 ). How this
    B> expression is manipulated. Kindly explain me in detail.

I don't have the Standard in front of me, and have been doing
bitshifting in Java lately, so it may be incorrect.

First, id will be cast to long long.  In your case, this is 64 bits,
although it may be more on other architectures.

Next, this long long is shifted 32 bits to the left.  This has the
effect of multiplying by (2^32), or 4294967296.  Since your original
value is 3, this comes out to 12884901888.

What happens next depends on what you're doing with this value.  For
example, if you have a variable, long long int foo, then the entire
value is kept and put into the new variable.  However, if you try to
stuff this into id (assuming sizeof(int) == 4 && CHAR_BIT=8, ie 32-bit
int), the high-end bits will be chopped off, leaving you with 0.

    B> TIA,
    B> Bryan

Cheers,
Kevin Lacquement

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey



Wed, 21 Jul 2004 17:05:56 GMT  
 typecasting int to long long int
======================================================
Quote:
>     B> ((long long int )id << 32 ).

>     B> What would be the result of this expression( assume id = 3 ). How this
>     B> expression is manipulated. Kindly explain me in detail.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The return value of the above expression is coming out as "ZERO". This
is what am wondering. as of K & R.. ffirst the typecasting has to be
done .. so id is converted into long long int and then it is shifted
left to 32 bits. WHen i tried this, it is returning zero, this is what
wondering me.

Am using printf with format operator as %x
Also long long int is a supported data type of size 8bytes.
Am doign it with gcc compiler in linux.
What could be the problem. I could not understand.
Kindly help me out.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Quote:
> I don't have the Standard in front of me, and have been doing
> bitshifting in Java lately, so it may be incorrect.

> First, id will be cast to long long.  In your case, this is 64 bits,
> although it may be more on other architectures.

> Next, this long long is shifted 32 bits to the left.  This has the
> effect of multiplying by (2^32), or 4294967296.  Since your original
> value is 3, this comes out to 12884901888.

-------------<snipped>------------------------


Thu, 22 Jul 2004 14:27:16 GMT  
 typecasting int to long long int

Quote:

> ======================================================
> >     B> ((long long int )id << 32 ).

> >     B> What would be the result of this expression( assume id = 3 ). How this
> >     B> expression is manipulated. Kindly explain me in detail.

> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

> The return value of the above expression is coming out as
> "ZERO".

It shouldn't.  It should return 3. * pow(2, 32) as an integer.

Quote:
> Am using printf with format operator as %x

The full specifier for a long long is %llx.  If you didn't get
that right then I can see how the output might be confusing.

[snippage]
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}



Thu, 22 Jul 2004 14:29:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. char size (was long long long long long int)

2. long long int

3. long long int into octets (and back again)

4. long long int

5. unsigned long long int

6. long long int

7. Assigning and printing long long int's

8. long int versus long

9. long long int

10. Help! INT longer than LONG

11. cast long int to short int

12. Conversion of signed long int to n-bit signed int and vs vs

 

 
Powered by phpBB® Forum Software