Integers' sizes problem

A

Andre

Hi,

Could someone tell me what's wrong with the following program? I think
there is something wrong with the size of integers I'm using, but not
sure. When I execute it, I get the following output, but the value of r
in 3 should be 0x000BFD0600000000.

Thanks,

Andre

***********************************************************************

1: 0000000000000000
2: 00000000000BFD06
3: 0000000000000000
4: 00000000A28B24BD
timeLow: A28B24BD
timeHigh: 000BFD06
time: 00000000A28B24BD -6734248419340058625

***********************************************************************

#include <stdio.h>

typedef int Int32;
typedef long long Int64;

inline Int64 form64( Int32 low, Int32 high )
{
Int64 r = 0;

printf("1: %016X\n", r);
r = high;
printf("2: %016X\n", r);
r = r<<32;
printf("3: %016X\n", r);
r = r|low;
printf("4: %016X\n", r);

return r;
}

int main()
{
Int32 timeLow = 0xA28B24BD;
Int32 timeHigh = 0x000BFD06;
Int64 time = form64(timeLow, timeHigh);

printf("timeLow: %08X\n", timeLow);
printf("timeHigh: %08X\n", timeHigh);
printf("time: %016X %lld\n\n", time, time);

return 0;
}
 
W

Walter Roberson

Could someone tell me what's wrong with the following program? I think
there is something wrong with the size of integers I'm using, but not
sure.
#include <stdio.h>
typedef int Int32;
typedef long long Int64;
inline Int64 form64( Int32 low, Int32 high )
{
Int64 r = 0;

printf("1: %016X\n", r);
r = high;
printf("2: %016X\n", r);
r = r<<32;
printf("3: %016X\n", r);
r = r|low;
printf("4: %016X\n", r);

return r;
}

typedef'ing long long as the name Int64 does not force long long to
actually -be- 64 bits. Yes, it -should- be for a C99 conforming
compiler, but are you sure you are using your compiler in full
C99 conformance mode? (And where did you find the C99 compiler
anyhow?) Are you certain that your compiler uses 64 bits for long long?
 
A

Arthur J. O'Dwyer

Could someone tell me what's wrong with the following program? I think
there is something wrong with the size of integers I'm using, but not
sure. When I execute it, I get the following output, but the value of r
in 3 should be 0x000BFD0600000000.

Ah... I had to actually run your program before I saw the bug, so
I don't blame you /too/ much. ;)

#include <stdio.h>
typedef long long Int64;
Int64 r = 0;
printf("3: %016X\n", r);

The "X" format specifier takes an unsigned int, not a signed long long.
Change this to
printf("3: %016llX\n", r);

Then your only remaining problems have to do with sign-extension, I
think. (Your Int32 is a signed type, so ORing something with an Int32
won't do what you expect if the high bit is 1.)

HTH,
-Arthur
 
M

Martin Ambuhl

Andre said:
Hi,

Could someone tell me what's wrong with the following program? I think
there is something wrong with the size of integers I'm using, but not
sure. When I execute it, I get the following output, but the value of r
in 3 should be 0x000BFD0600000000.

You are doing a few wrong things several times.
1) You are using %X specifiers, for which the corresponding parameter
should be an unsigned int, but you are using them for signed long longs.
You have both the signedness and the size wrong.
2) You are doing binary operations (ORs and shifts) on signed numbers.
This is an error.

Below, losing the silly typedefs so things become clearer, is working
version of your program.

#include <stdio.h>


inline unsigned long long form64(unsigned int low, unsigned int high)
{
unsigned long long r = 0;

printf("1: %016llX\n", r); /* mha: fixed format specifier */
r = high;
printf("2: %016llX\n", r); /* mha: fixed format specifier */
r = r << 32;
printf("3: %016llX\n", r); /* mha: fixed format specifier */
r = r | low;
printf("4: %016llX\n", r); /* mha: fixed format specifier */

return r;
}

int main()
{
unsigned int timeLow = 0xA28B24BD;
unsigned int timeHigh = 0x000BFD06;
unsigned long long time = form64(timeLow, timeHigh);

printf("timeLow: %08X\n", timeLow);
printf("timeHigh: %08X\n", timeHigh);
printf("time: %016llX %llu\n\n", time, time);
/* mha: fixed two format specifiers above */

return 0;
}



[output]

1: 0000000000000000
2: 00000000000BFD06
3: 000BFD0600000000
4: 000BFD06A28B24BD
timeLow: A28B24BD
timeHigh: 000BFD06
time: 000BFD06A28B24BD 3374429682476221
 
K

Keith Thompson

Andre said:
#include <stdio.h>

typedef int Int32;
typedef long long Int64;

inline Int64 form64( Int32 low, Int32 high )
{
Int64 r = 0;

printf("1: %016X\n", r);
r = high;
printf("2: %016X\n", r);
r = r<<32;
printf("3: %016X\n", r);
r = r|low;
printf("4: %016X\n", r);

return r;
}

int main()
{
Int32 timeLow = 0xA28B24BD;
Int32 timeHigh = 0x000BFD06;
Int64 time = form64(timeLow, timeHigh);

printf("timeLow: %08X\n", timeLow);
printf("timeHigh: %08X\n", timeHigh);
printf("time: %016X %lld\n\n", time, time);

return 0;
}

You've created typedefs "Int32" and "Int64", but your printf formats
still work only for the underlying types. (And, as Andrew pointed
out, some of your formats are still incorrect.)

You probably want to use unsigned types rather than signed types.
Bitwise operations on signed types rarely make sense; I haven't even
bothered to learn the rules about when their behavior is undefined.

If you want specific sizes, use the types defined in <stdint.h> if
it's available. You can declare similar typedefs yourself if your
ipmlementation doesn't provide <stdint.h>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top