473,903 Members | 6,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assigning 64-bit hex values to unsigned long variables

4 New Member
I'm having a problem with assigning 64-bit hex values to unsigned long variables in MS VS2005 c++ compiler.

Expand|Select|Wrap|Line Numbers
  1. unsigned long Number;
  2. Number = 0x1000000000000000UL;
After this declaration and assignment, the value in Number is always 0. However, if the hex value is 0x0000000000000 001, then the value is 1 as expected. It seems like the compiler is truncating the most significant 32 bits. How can I make this work?
Mar 28 '09 #1
9 16085
JosAH
11,448 Recognized Expert MVP
@shortyzms
Maybe you should try:

Expand|Select|Wrap|Line Numbers
  1. unsigned long long Number;
  2.  
kind regards,

Jos
Mar 29 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Good Luck. sizeof(unsigned long long) is 8. That is, VS2005 is a 32-bit compiler.

The OP should be using a 64-bit compiler.
Mar 29 '09 #3
donbock
2,426 Recognized Expert Top Contributor
@JosAH
And
Expand|Select|Wrap|Line Numbers
  1. Number = 0x1000000000000000ULL;
To the OP: what gave you the impression that your compiler supports 64-bit integers? The C Standard only requires 'long' and 'unsigned long' to support 32-bit integers. Compilers are permitted to support wider integers, but a portable program shouldn't rely on such largesse.

C99 (and C++?) added 'long long' and 'unsigned long long'. Don't know about C++, but C99 requires these types to be at least 64-bits wide. Refer to <limits.h> for your particular compiler's type sizes.
Mar 30 '09 #4
donbock
2,426 Recognized Expert Top Contributor
@weaknessforcats
I think a sizeof of 8 is 64 bits.
Mar 30 '09 #5
shortyzms
4 New Member
Thanks for the tip JoS, it worked like a charm!


@donbock
I was given the impression that 'long' always meant 64 bits. I didn't know that the standard only requires 'unsigned long' to support numbers in the range of an unsigned 32-bit integer.

C99 (and C++?) added 'long long' and 'unsigned long long'. Don't know about C++, but C99 requires these types to be at least 64-bits wide. Refer to <limits.h> for your particular compiler's type sizes.
Thanks for this tip!
Mar 31 '09 #6
Banfa
9,065 Recognized Expert Moderator Expert
@weaknessforcats
It may be but long long equates to a 64bit integer on it in the same way that a decade or so ago long was a 32bit integer even when the compiler was compiling for a 16bit target (actually that is still true for all those 16bit microprocessor that are still being manufactured)

Try this on you VS2005 install

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. int main(int argc, char **argp)
  5. {
  6.     unsigned long long Number = ULLONG_MAX;
  7.  
  8.     printf("Number: size(%d) Value=%I64x\n", sizeof Number, Number);
  9.  
  10.     return 0;
  11. }
  12.  
Apr 1 '09 #7
Banfa
9,065 Recognized Expert Moderator Expert
@shortyzms
Actually the standard (for C) only requires that

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

and

sizeof(char) == 1

C++ is a little more stringent than this. Additionally the number of bits in a char is variable (normally dependent on the hardware platform) although 8 bits in a char is far and away the most common configuration.

From that you can see that it would be possible to have a platform where all integers where 8 bit although that would certainly not be useful.

Again most commonly

a byte has 8 bits
sizeof(char) == 1 (by definition a char is 1 byte)
sizeof(short) == 2
sizeof(int) == 2 or 4
sizeof(long) == 2 or 4

The only slight problem is that int is supposed to be the size of integer that the platform can process most easily, hence it is 2 or 4 bytes depending on if the platform is 16 or 32 bit. Strictly speaking that means that on modern 64 bit platforms sizeof(int) should be 8 (and therefore sizeof(long) should be 8 or greater) but most of the compiler manufacturers didn't do this I believe because they where afraid of breaking everyone else's software.

In case you are wondering C++ adds the additional specifications that short and int should be a minimum of 16 bits and long should be a minimum of 32 bits (I think).
Apr 1 '09 #8
donbock
2,426 Recognized Expert Top Contributor
This note is excessively pedantic ... but I love that stuff!

Section 5.2.4.2.1 of C89 (um ... I'm not sure if that's ANSI or ISO section numbering ... if you know what I mean by this comment then congratulations , you're an official C pedant) sets minimum limits on the sizes of the integer types. The "implementa tion-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."

minimum value for an object of type signed char: -127

maximum value for an object of type signed char: +127 (0x7F)

maximum value for an object of type unsigned char: 255 (0xFF)

minimum value for an object of type short int: -32767

maximum value for an object of type short int: +32767 (0x7FFF)

maximum value for an object of type unsigned short int: 65535 (0xFFFF)

minimum value for an object of type long int: -2147483647

maximum value for an object of type long int: +2147483647 (0x7FFFFFFF)

maximum value for an object of type unsigned long int: 4294967295 (0xFFFFFFFF)

From these numerical limits it is clear that char must be at least 8 bits wide, short must be at least 16 bits wide, and long must be at least 32 bits wide.

The necessary relative sizes of these types are just as Banfa stated, however that relationship is specified elsewhere in C89, and I'm not taking the time to track it down.
Apr 6 '09 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
The above is only true for ASCII as the C implementation character set. This covers 99.999% of the C implementations .

However, if your compiler uses another character set, the above are not true. Some microprocessors used by Boeing to simulate the 757 cockpit did not use ASCII. They had a version of TinyC.
Apr 7 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6417
by: gtz669 | last post by:
I have a class which I am using for data stroage. I declare an instance of that class in my main class which is running my java applet. I Iassign it a value in the init () function and it works fine but when I try to do the same assignment later on, it restults in a NullPointerException and I can not figure this out. Could someone please help. Thanks. Here's the code: public class A extends java.applet.Applet
1
2061
by: Hugo | last post by:
I have a dual boot machine, runs Win XP pro and Win XP Pro 64, XP boots from C and XP 64 boots from D. They both have VS 2005 Beta 2 installed. I have a webapp developed in Win XP (32) which has gone well and runs fine in XP. I Now want to get this same app running under IIS on XP 64.
56
4153
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation error. (Not as infrequent as some bugs that have been discussed here in the past, but maybe once in an overnight run of the program when it was configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
0
1311
by: Hugo | last post by:
I have a dual boot machine, runs Win XP pro and Win XP Pro 64, Win XP boots from C and Win XP 64 boot from D. They both have VS 2005 Beta 2 installed. I have a webapp developed in Win XP (32) which has gone well and runs fine in XP. I Now want to get this same app running under IIS on XP 64.
1
2174
by: mel_apiso | last post by:
Hi, we have an AIX 5.3 OS, and we purchased DB2 UDB version 8 Workgroup Edition. We want to install 64 bits version, but the source CD's that we have say: WORKGROUP SERVER EDITION Version 8.1 for AIX 5L
10
9489
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m < 4) str = "%01x"; else if (m < 9) str = "%02x"; else str = "%03x";
13
4101
by: Mary Lei | last post by:
Does anyone know the link to obtain the tarball for db2 8.1 for solaris running on AMD 64 bit ? This is the entire db2 installation on a new system that does not have db2. Thanks.
1
1773
by: speralta | last post by:
For some reason the text in h2 tag is displaying as white in IE. http://www.salperalta.com/ <td class="sidebar" id="sidebar-right"> <div class="block block-listing" id="block-listing-0"> <h2>Easy MLS Search for Oregon and Southwest Washington:</h2> <div class="content"> </div> </div>
1
2376
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 found and has been sent to me is an Invalid_Viewstate exception. I will provide the stack trace below. If you read down the stack trace it talks about "Invalid chararcter in a base-64
1
1535
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is available (i.e. using an adapter, or something like the strategy pattern). To give an example and a non-example of what I'm talking about, consider the following recipes from the online Python Cookbook: Ring Buffer:...
0
10882
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10504
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9692
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
8057
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
5899
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
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4734
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
4316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.