473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is long long int not as long as promised??

I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:

for (i=0; i<64; i++){
long long int k = 1<<i;

cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
results in

1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
......

and so on.

I'm beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.

Thanks

Oliver
Sep 25 '08 #1
15 2757
Oliver Graeser wrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:

for (i=0; i<64; i++){
long long int k = 1<<i;
unsinged long int k = 1<<i; // use this
>
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
That happens because you have only half of the numbers for positive, the
other half is used for negatives.

Look here: http://www.cplusplus.com/doc/tutorial/variables.html
Sep 25 '08 #2
Oliver Graeser wrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:

for (i=0; i<64; i++){
long long int k = 1<<i;

cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
results in

1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....

and so on.

I'm beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.

Thanks

Oliver
1 is an int, so when you shift it left by more than 32 you wrap around. The
(int) result fits nicely into a 'long long', so the compiler has no reason
to complain. Try this:

const long long int ONE=1L;
for (i=0; i<64; i++){
long long int k = ONE << i;
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Sep 25 '08 #3
On Thu, 25 Sep 2008 21:45:13 +0800, Oliver Graeser wrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
Maybe your 64 bit "int long long" holds positive and negative numbers.
Is there an "unsigned" type you can use?

Sep 25 '08 #4
Oliver Graeser wrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:

for (i=0; i<64; i++){
long long int k = 1<<i;
Try changing this to

long long int k = 1LL << i; // note the suffix after '1'.

You were shifting a simple int (1 is of type int), and its range is more
limited than that of 'long long'.
>
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
results in

1 2 8
2 4 8
3 8 8
4 16 8
5 32 8
6 64 8
7 128 8
8 256 8
9 512 8
10 1024 8
11 2048 8
12 4096 8
13 8192 8
14 16384 8
15 32768 8
16 65536 8
17 131072 8
18 262144 8
19 524288 8
20 1048576 8
21 2097152 8
22 4194304 8
23 8388608 8
24 16777216 8
25 33554432 8
26 67108864 8
27 134217728 8
28 268435456 8
29 536870912 8
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....

and so on.

I'm beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.

Thanks

Oliver
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '08 #5
In message <vd************ *************** ***@speakeasy.n et>, A. W.
Dunstan <no@spam.thanks writes
>Oliver Graeser wrote:
>I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:

for (i=0; i<64; i++){
long long int k = 1<<i;

cout<<i<<"\t"< <k<<"\t"<<sizeo f(k)<<"\n";
}
results in

1 2 8
....
>30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....

and so on.

I'm beyond confused - can anyone here help me out? Machine is a Core 2
Duo Macbook Pro with OSX10.5, gcc, Xcode.

1 is an int, so when you shift it left by more than 32 you wrap around.
Worse. When you shift it left by 32 or more, you get UB.

(assuming 'int' is 32 bits on this machine.)

5.8/1:
"The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left
operand."
The
(int) result fits nicely into a 'long long', so the compiler has no reason
to complain. Try this:

const long long int ONE=1L;
That would be a 'long' literal, not 'long long'.
>for (i=0; i<64; i++){
long long int k = ONE << i;
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
--
Richard Herring
Sep 25 '08 #6
In article <gb**********@i justice.itsc.cu hk.edu.hk>, Oliver Graeser
<gr*****@phy.cu hk.edu.hkwrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte =
64 bit right?
long long isn't part of C++. Since you're relying on compiler extensions,
you might as well use int64_t from <stdint.hif you want a 64-bit
integer, or int_least64_t if 64 bits or greater is acceptable.
Sep 25 '08 #7
Richard Herring wrote:
In message <vd************ *************** ***@speakeasy.n et>, A. W.
Dunstan <no@spam.thanks writes
[..]
> The
(int) result fits nicely into a 'long long', so the compiler has no
reason
to complain. Try this:

const long long int ONE=1L;

That would be a 'long' literal, not 'long long'.
That actually doesn't matter. It could be

const long long int ONE = 1;

or

const long long int ONE = '\1';

the conversion should still yield a suitable value in 'ONE'.
>for (i=0; i<64; i++){
long long int k = ONE << i;
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '08 #8
Richard Herring wrote:
>1 is an int, so when you shift it left by more than 32 you wrap around.

Worse. When you shift it left by 32 or more, you get UB.

(assuming 'int' is 32 bits on this machine.)

5.8/1:
"The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left operand."
That's actually something which should be taken seriously. I remember
back when I was developing on some UltraSparc architecture that if you
shifted an integer by more bits than there was in the integer, the
result would be, IIRC, the original value of that integer (rather than
zero, like AFAIK happens with intel CPUs). In other words, if the amount
of shift was invalid, the UltraSparc would simply not execute it
(leaving the original value of the integer intact). This can come as a
surprise to many.
Sep 25 '08 #9
On Sep 25, 6:41 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message <vd2dnRWiHbWCBE bVnZ2dnUVZ_tfin ...@speakeasy.n et>, A. W.
Dunstan <n...@spam.than kswrites
Oliver Graeser wrote:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte=
64 bit right? but when I try to assign a value with more than 32 bit,
it fails. To illustrate:
for (i=0; i<64; i++){
long long int k = 1<<i;
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
results in
1 2 8
...
30 1073741824 8
31 -2147483648 8
32 1 8
33 2 8
34 4 8
35 8 8
36 16 8
.....
and so on.
I'm beyond confused - can anyone here help me out? Machine
is a Core 2 Duo Macbook Pro with OSX10.5, gcc, Xcode.
1 is an int, so when you shift it left by more than 32 you
wrap around.
Worse. When you shift it left by 32 or more, you get UB.
And it certainly doesn't wrap.

(But am I the only one who absolutely refuses to use shift
operators on signed types.)
(assuming 'int' is 32 bits on this machine.)
5.8/1:
"The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left
operand."
The
(int) result fits nicely into a 'long long', so the compiler has no reason
to complain. Try this:
const long long int ONE=1L;
That would be a 'long' literal, not 'long long'.
But ONE would be an integral constant of type long long.
for (i=0; i<64; i++){
long long int k = ONE << i;
cout<<i<<"\t"<< k<<"\t"<<sizeof (k)<<"\n";
}
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 25 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
100265
by: Tim Clacy | last post by:
How is a 64 bit type defined in strict C++? It seems C has support for 'long long' since C99, but not so for C++? Looking through one compiler vendor's standard library headers has clouded the issue somewhat; it uses __int64 in some places (with a comment about strict) but freely uses 'long long' in other places. In any case, these standard library headers are supposed to be 'standard' w.r.t. C and C++ aren't they? How can a standard...
7
3605
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
1
1656
by: Crimarc | last post by:
I come back to ask qustion what did he promised you that you soudned so much happy and sometime later so painful like you heart broken becase of his dificults ???? What did you sacrifice for C++ things, means how much effort you spend for c++ so far to becaome takable like a professional ? I know C++ overloading functions always hardest to write. i am love learn about overloading. Do you know of any good easy tutirals for me to learn...
5
4811
by: Mark Shelor | last post by:
Problem: find a portable way to determine whether a compiler supports the "long long" type of C99. I thought I had this one solved with the following code: #include <limits.h> #ifdef ULONG_LONG_MAX
9
3949
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
21
2822
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be convenient (and for some things possibly necessary) to use long long integer and/or unsigned long long integer variables. How widely supported are these variable types? How long ago were they introduced? I notice they are not mentioned in K&R #2.
12
13500
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used sprintf(pHex,"%0X",9987967441778573855). But it only returns 8
2
7279
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a single line with "\n" in the middle, or I could use multiple cout and endl. But I just feel more readable if I can have the whole paragraph as a string. Thanks,
10
3829
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.