473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using 64-bit integers on 32-bit machine

Hi,

I'm trying to use uint64_t by #including stdint.h in C on 32-bit
machine. However, I can't seem to get the right value for shifting,
for example:

for(i = 0; i < 64; i++)
{
c = 1 << i;
printf(" %d : %x\n", i, c);
}

gives the following output

0 : 1
1 : 2
2 : 4
3 : 8
4 : 10
5 : 20
6 : 40
7 : 80
8 : 100
9 : 200
10 : 400
11 : 800
12 : 1000
13 : 2000
14 : 4000
15 : 8000
16 : 10000
17 : 20000
18 : 40000
19 : 80000
20 : 100000
21 : 200000
22 : 400000
23 : 800000
24 : 1000000
25 : 2000000
26 : 4000000
27 : 8000000
28 : 10000000
29 : 20000000
30 : 40000000
31 : 80000000
32 : 1
33 : 2
34 : 4
35 : 8
36 : 10
37 : 20
38 : 40
39 : 80
40 : 100
41 : 200
42 : 400
43 : 800
44 : 1000
45 : 2000
46 : 4000
47 : 8000
48 : 10000
49 : 20000
50 : 40000
51 : 80000
52 : 100000
53 : 200000
54 : 400000
55 : 800000
56 : 1000000
57 : 2000000
58 : 4000000
59 : 8000000
60 : 10000000
61 : 20000000
62 : 40000000
63 : 80000000
Anyone know what the problem is, and what the correct solution is for
such use?
Thanks,
-Seung

Sep 28 '07 #1
5 7479
seung wrote:
Hi,

I'm trying to use uint64_t by #including stdint.h in C on 32-bit
machine. However, I can't seem to get the right value for shifting,
for example:
What's wrong with the answers you received on comp.std.c?

--
Ian Collins.
Sep 28 '07 #2
seung wrote:
I'm trying to use uint64_t by #including stdint.h in C on 32-bit
machine. However, I can't seem to get the right value for shifting,
for example:

for(i = 0; i < 64; i++)
{
c = 1 << i;
printf(" %d : %x\n", i, c);
}
c = 1ULL << i;
printf("%d : %llx\n", i, c);
(Assuming uint64_t c)
Sep 28 '07 #3
seung wrote:
Hi,

I'm trying to use uint64_t by #including stdint.h in C on 32-bit
machine. However, I can't seem to get the right value for shifting,
for example:

for(i = 0; i < 64; i++)
{
c = 1 << i;
printf(" %d : %x\n", i, c);
}

gives the following output
[snipped]

Anyone know what the problem is, and what the correct solution is for
such use?
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>

int main(void)
{
unsigned i;
uint64_t c;

printf("For this implementation,\n"
"sizeof(uint64_t) = %zu, * CHAR_BIT = %zu\n"
"and the macro PRIx64 expands to " PRIx64
".\n Check to make sure these are OK.\n\n", sizeof(uint64_t),
sizeof(uint64_t) * CHAR_BIT);
for (i = 0; i < 64; i++) {
c = 1llu << i;
printf(" %u : %" PRIx64 "\n", i, c);
}
return 0;
}

[output]
For this implementation,
sizeof(uint64_t) = 8, * CHAR_BIT = 64
and the macro PRIx64 expands to llx.
Check to make sure these are OK.

0 : 1
1 : 2
2 : 4
3 : 8
4 : 10
5 : 20
6 : 40
7 : 80
8 : 100
9 : 200
10 : 400
11 : 800
12 : 1000
13 : 2000
14 : 4000
15 : 8000
16 : 10000
17 : 20000
18 : 40000
19 : 80000
20 : 100000
21 : 200000
22 : 400000
23 : 800000
24 : 1000000
25 : 2000000
26 : 4000000
27 : 8000000
28 : 10000000
29 : 20000000
30 : 40000000
31 : 80000000
32 : 100000000
33 : 200000000
34 : 400000000
35 : 800000000
36 : 1000000000
37 : 2000000000
38 : 4000000000
39 : 8000000000
40 : 10000000000
41 : 20000000000
42 : 40000000000
43 : 80000000000
44 : 100000000000
45 : 200000000000
46 : 400000000000
47 : 800000000000
48 : 1000000000000
49 : 2000000000000
50 : 4000000000000
51 : 8000000000000
52 : 10000000000000
53 : 20000000000000
54 : 40000000000000
55 : 80000000000000
56 : 100000000000000
57 : 200000000000000
58 : 400000000000000
59 : 800000000000000
60 : 1000000000000000
61 : 2000000000000000
62 : 4000000000000000
63 : 8000000000000000
Sep 28 '07 #4
On Fri, 28 Sep 2007 10:16:19 +0000, seung wrote:
Hi,

I'm trying to use uint64_t by #including stdint.h in C on 32-bit
machine. However, I can't seem to get the right value for shifting,
for example:

for(i = 0; i < 64; i++)
{
c = 1 << i;
printf(" %d : %x\n", i, c);
}

gives the following output

0 : 1
1 : 2
2 : 4
[snip]
30 : 40000000
31 : 80000000
32 : 1
33 : 2
[snip]
62 : 40000000
63 : 80000000
Anyone know what the problem is, and what the correct solution is for
such use?
Note that 1 << i is evaluated to its type (the type resulting from
integer promotions of 1, which is an int, and i, whose declaration
you didn't show), and only then it is converted to the type of c.
Replace 1 with 1ULL.
Also "%x" is the conversion specifier for unsigned int (often 32
bits even on 64-bits machines), for uint64_t you need the macro
PRIx64 (e.g. " %d : %" PRIx64 "\n"), most likely to be "llx" for
unsigned long long.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 29 '07 #5
On Sat, 29 Sep 2007 17:59:41 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
On Fri, 28 Sep 2007 10:16:19 +0000, seung wrote:
<snip: uint64_t>
c = 1 << i;
Note that 1 << i is evaluated to its type (the type resulting from
integer promotions of 1, which is an int, and i, whose declaration
you didn't show), and only then it is converted to the type of c.
Each of 1 and i is subject to integer promotions, yes. However, your
'and' seems to imply there is some relation _between_ them.

_Most_ arithmetic operators do what are called the usual arithmetic
conversions: each operand separately is subject to integer promotions
and then if they are (still) different types they are both converted
to, informally, a 'least common' type; usually this is the larger of
the two, so only the smaller is really converted.
E.g.: 3 + 4L the 3 is converted to long to match the 4L.

But not shifts. They only do integer promotions. In 1 << i the 1
remains int even if i is long long or int2048_t.
Replace 1 with 1ULL.
Or (uint64_t)1 . Which more clearly represents the intent, and avoids
the risk, albeit a very tiny one, that long long is more than 64 bits.
Also "%x" is the conversion specifier for unsigned int (often 32
bits even on 64-bits machines), for uint64_t you need the macro
PRIx64 (e.g. " %d : %" PRIx64 "\n"), most likely to be "llx" for
unsigned long long.
Right.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 14 '07 #6

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

Similar topics

2
by: Ben | last post by:
Hi. I have a button that change a number of images src's when I click a button. The src's are stored in an array and I just use document.src=pics to change the src of the image. However I want...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
7
by: Simon Jefferies | last post by:
Hello, How do I use a C++ library in a C# project? Can it be done under 2005? Do I need to anything special, I remember reading somewhere that they included this feature in the latest .NET. ...
2
by: Mehr H | last post by:
I have been working on this for several days and am still have had no success in achieving this. Pleae help. It seems that documentation for this is very limited. I have looked in several books and...
1
by: Mehr H | last post by:
I have been working on this for several days and am still have had no success in achieving this. Pleae help. It seems that documentation for this is very limited. I have looked in several books and...
2
by: theinsanecoder | last post by:
Hi, i'm using the xmlserializer to load and unload data from my files but i want to crypt them using a not so secure cryptographic encoder. My code to encrypt "seems" to work fine, the file is...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
1
by: =?Utf-8?B?UGF1bCBQaGlsbGlwcw==?= | last post by:
I have read many things about this but I haven't got a clear vision on what to do if anything about this. I have a system that tries to find holes in my web site. One of the things it has...
0
by: toto1980 | last post by:
Hi; I tried look for a simple way of sending a file using sockets, I found one in the old forums but I am not sure why the file when it is sent, the client side some how receive wrong contents, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.