Assigning and printing long long int's 
Author Message
 Assigning and printing long long int's

Using gcc under DOS, I seem unable to assign values greater than 2^32
for initializing unsigned long long int's, nor am I able to print
them.  In either case, the intended values are always reduced mod 2^32.

If I want to assign, say, the initial value 2^33+2^16  (math notation)
to
an unsigned long long t, and give the decimal equivalent, 8590000128,
gcc will give a warning, then take only the rightmost 32 bits, making
t=65536.
(Nor will  t = 8590000128LL;   yield other than  65536 for t.)
If I use the artifice of getting the required values into t via
intermediate assignments to
doubles x,y---for example, x=8590000128.0; t=x; y=(t<<20);---  then
arithmetic
and  logical operations on the  resulting 64 bit long longs seem to work
as expected,
although it seems necesary to  use printf(" %20.0f",y)  to get the
content of t.   Using
printf("%u",t)  gives only the rightmost 32 bits of t.

The long long int type does not seem as thoroughly discussed as
 older types in the C FAQs, or in K&R or the other  C book I have.

In what  ways can one get intended 64 bit-patterns into long long int's
other than through casts  from other or mixed types, or by arithmetic on

long longs with declared values in the 32-bit range?

And how can the full 64-bit content be printed through direct use of  %
formats?

Is this only a problem for djgpp gcc?

George Marsaglia



Sat, 19 Jul 2003 00:52:48 GMT  
 Assigning and printing long long int's

Quote:
>Using gcc under DOS, I seem unable to assign values greater than 2^32
>for initializing unsigned long long int's, nor am I able to print
>them.  In either case, the intended values are always reduced mod 2^32.

since %lu prints a long version of an integer, maybe %llu prints the long
long version of an integer. Regardless, your compiler manual should tell you
how to do this.


Sat, 19 Jul 2003 01:40:17 GMT  
 Assigning and printing long long int's


Quote:
>If I want to assign, say, the initial value 2^33+2^16  (math notation)
>to an unsigned long long t, and give the decimal equivalent, 8590000128,
>gcc will give a warning ...

GCC is not (yet) a C99 compiler but I believe all modern versions
do handle this constant in a C99-conformant manner.  You can
eliminate the warning by using the "LL" suffix.  (C99 does not
require a warning here; gcc is just being friendly.)

Quote:
>... then take only the rightmost 32 bits, making t=65536.

This, I suspect, is not actually the case.  It is a bit hard to
tell what goes into "t" without looking at the machine code, but
this:

Quote:
>... Using printf("%u",t)

will certainly not show the full value in "t".  The %u directive
expects an unsigned int.

Quote:
>... gives only the rightmost 32 bits of t.

Since GCC uses this target machine's stack, and this target machine
is little-endian, that is what I would expect from this call
(formally this call yeilds undefined behavior, but in this case I
know enough about the compiler and machine to predict the result).

The C99 format for printing a "long long unsigned" is "%llu".
Whether the library you are using with this version of GCC is
conformant in this respect, I have no idea.  Using "%llu" is certainly
worth a try though.  (Another common format is "%qu".)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sat, 19 Jul 2003 05:20:35 GMT  
 Assigning and printing long long int's
On Mon, 29 Jan 2001 11:52:48 -0500, George Marsaglia

Quote:
> Using gcc under DOS, I seem unable to assign values greater than 2^32
> for initializing unsigned long long int's, nor am I able to print
> them.  In either case, the intended values are always reduced mod 2^32.

> If I want to assign, say, the initial value 2^33+2^16  (math notation)
> to
> an unsigned long long t, and give the decimal equivalent, 8590000128,
> gcc will give a warning, then take only the rightmost 32 bits, making
> t=65536.
> (Nor will  t = 8590000128LL;   yield other than  65536 for t.)
> If I use the artifice of getting the required values into t via
> intermediate assignments to
> doubles x,y---for example, x=8590000128.0; t=x; y=(t<<20);---  then
> arithmetic
> and  logical operations on the  resulting 64 bit long longs seem to work
> as expected,
> although it seems necesary to  use printf(" %20.0f",y)  to get the
> content of t.   Using
> printf("%u",t)  gives only the rightmost 32 bits of t.

> The long long int type does not seem as thoroughly discussed as
>  older types in the C FAQs, or in K&R or the other  C book I have.

> In what  ways can one get intended 64 bit-patterns into long long int's
> other than through casts  from other or mixed types, or by arithmetic on

> long longs with declared values in the 32-bit range?

> And how can the full 64-bit content be printed through direct use of  %
> formats?

> Is this only a problem for djgpp gcc?

> George Marsaglia

Source:

#include <stdio.h>

int main(void)
{
   long long ll = 0x0000000180000000;
   printf("%lld\n", ll);
   return 0;

Quote:
}

Output screen:

This is RHIDE Version 1.4. Copyright (c) 1996,1997 by Robert H?hne
             (Sep 30 1997 23:06:59)
6442450944
====================
As others have said, "%u" is _not_ "%lld".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 19 Jul 2003 13:34:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

2. typecasting int to long long int

3. long long long long integers

4. Assigning 4 bytes from a buffer into a long int

5. long long int

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

7. long long int

8. unsigned long long int

9. long long int

10. long int versus long

11. long long int

12. Help! INT longer than LONG

 

 
Powered by phpBB® Forum Software