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;
} 4 2696
In article <11**********************@l12g2000cwl.googlegroups .com>,
Andre <si******@yahoo.comwrote:
>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?
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
On Wed, 6 Dec 2006, Andre wrote:
>
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
Andre wrote:
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
"Andre" <si******@yahoo.comwrites:
[...]
#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.hif
it's available. You can declare similar typedefs yourself if your
ipmlementation doesn't provide <stdint.h>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: j0mbolar |
last post by:
I've read in the standard that addresses
basically can't be interpreted as integers.
If they can, it is implementation defined
behavior. However, if they can't be viewed
as integers in any sense...
|
by: none |
last post by:
If I need to add up all integers in a line (they're separated by space)
How can I do that??
e.g.
input : 1 4 5 2 3
sum : 15
input : 2 3
sum: 5
|
by: junky_fellow |
last post by:
I have a question related to pointer/integer conversion.
After reading various threads on this newsgroup I found that
pointer/integer conversion is not portable and may lead to
undefined results...
|
by: Martin Joergensen |
last post by:
Hi,
I have some files which has the following content:
0 0 0 0 0 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
|
by: Michael Brennan |
last post by:
Hi,
I wonder if there is any good reason to let different systems have
different sizes of short, int and long?
I think that makes it harder to plan which type to use for your program.
For...
|
by: deko |
last post by:
As I understand it, most browser manufacturers have agreed on 16px for their
default font size.
So, this should be an accurate conversion for percentages:
px %
16 = 100
14 = 87.5
13 =...
|
by: SpreadTooThin |
last post by:
I am used to coding by defining my own integer data types. For
example:
u8bit is unsigned char
u16bit is unsigned short
u32bit is unsigned long or int
With the introduction of 64 bit...
|
by: Juha Nieminen |
last post by:
I was once taught that if some integral value can never have negative
values, it's a good style to use an 'unsigned' type for that: It's
informative, self-documenting, and you are not wasting half...
|
by: darren |
last post by:
Hi
I have an assignment that is in C and, for an API call, asks for a
uint_16 and uint_32 in one of its functions.
In my C++ code i've been using uint16_t and uint32_t for fixed length...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |