473,804 Members | 4,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int64 from int 32

Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?

Deepak
Nov 12 '08 #1
22 4767
Peerless <pe************ *@gmail.comwrit es:
Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?
The %x specifier for printf expects an unsigned argument, not a long
long. Using %llx will use an unsigned long long. So you might want

unsigned long long big;
big = ((unsigned long long)hi) << 32 | low;
printf("%16llx\ n", big);

This conforms to the C99 standard, by the way, so it shouldn't be
compiler dependent.

Nov 12 '08 #2


Peerless wrote:
Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?
Support for 'long long' is compiler dependent, because not all
compilers conform to the C99 standard in that regard yet. However, if
that had been your problem, your code would never have compiled.

I strongly recommend using unsigned types whenever you are performing
shifts or bit-masking operations. However, for the particular values
involved in your program, that's not a problem.

What is a problem is that 'big' is a "long long", and you're using
%16x as your format code, which requires an "unsigned int" argument.
After changing everything to an unsigned type, you should use
"%16llx", which expects an "unsigned long long" argument.
Nov 12 '08 #3
Peerless <pe************ *@gmail.comwrit es:
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?
"%16x" expects an argument of type unsigned int. You're giving it an
argument of type long long. Use "%16llx". (It's not certain that
your runtime library will support this.)

Actually, I'd probably use "%016llx" to get the leading zeros.

Also, why do you have spaces before and after the "\n"?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 12 '08 #4
Nate Eldredge <na**@vulcan.la nwrites:
[...]
The %x specifier for printf expects an unsigned argument, not a long
long. Using %llx will use an unsigned long long. So you might want

unsigned long long big;
big = ((unsigned long long)hi) << 32 | low;
printf("%16llx\ n", big);

This conforms to the C99 standard, by the way, so it shouldn't be
compiler dependent.
Um, are you suggesting that all compilers conform to the C99 standard?

Quibble: It's the runtime library, not the compiler, that implements
printf and the "%llx" format. Both the library and the compiler are
part of the implementation.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 12 '08 #5
Keith Thompson <ks...@mib.orgw rote:
Nate Eldredge <n...@vulcan.la nwrites:
...
unsigned long long big;
big = ((unsigned long long)hi) << 32 | low;
printf("%16llx\ n", big);

This conforms to the C99 standard, by the way, so
it shouldn't be compiler dependent. *

Um, are you suggesting that all compilers conform to
the C99 standard?

Quibble: It's the runtime library, not the compiler,
that implements printf and the "%llx" format. *Both
the library and the compiler are part of the
implementation.
Do you know of any packaged C90 implementations that
support long long but don't support ll? It's been a
well over a decade since I've seen a C90 implementation
use L as the length specifier for long long.

--
Peter
Nov 12 '08 #6
Peter Nilsson <ai***@acay.com .auwrites:
Keith Thompson <ks...@mib.orgw rote:
>Nate Eldredge <n...@vulcan.la nwrites:
...
unsigned long long big;
big = ((unsigned long long)hi) << 32 | low;
printf("%16llx\ n", big);

This conforms to the C99 standard, by the way, so
it shouldn't be compiler dependent. *

Um, are you suggesting that all compilers conform to
the C99 standard?

Quibble: It's the runtime library, not the compiler,
that implements printf and the "%llx" format. *Both
the library and the compiler are part of the
implementation .

Do you know of any packaged C90 implementations that
support long long but don't support ll? It's been a
well over a decade since I've seen a C90 implementation
use L as the length specifier for long long.
Packaged? No. But gcc, for example, is just a compiler; it isn't
bundled with a runtime library. It uses whatever C runtime library is
available on the platform. *If* there are still runtime libraries
that doesn't support "%llx" (perhaps because the don't support long
long at all), and if gcc is available on such sytems, then there are C
implementations that support long long but not "%llx".

I'm sure there have been such systems in the past. I couldn't tell
you which systems they are, and I don't know whether they're still in
sufficiently widespread use to be a significant concern.

gcc may not be the only example of this.

Bottom line: Check this before depending on it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 12 '08 #7
Peerless wrote:
Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?

Deepak
Using the lcc-win compiler I obtain:
d:\lcc\mc74\tes t>lc tll.c
Warning tll.c: 11 printf argument mismatch for format x. Expected int
got long long
0 errors, 1 warning

It helps to get a compiler with good warnings.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 12 '08 #8
On Wed, 12 Nov 2008 23:12:18 +0100,
jacob navia <ja***@nospam.c omwrote:
Peerless wrote:
>Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio .h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?

Deepak

Using the lcc-win compiler I obtain:
d:\lcc\mc74\tes t>lc tll.c
Warning tll.c: 11 printf argument mismatch for format x. Expected int
got long long
Isn't the compiler supposed to expect an unsigned int here, instead of
an int?
0 errors, 1 warning

It helps to get a compiler with good warnings.
FWIW, gcc also warns for this, but it claims to be expecting an
unsigned int.

Martien
--
|
Martien Verbruggen | Failure is not an option. It comes bundled
| with your Microsoft product.
|
Nov 12 '08 #9
Peerless wrote:
Hi,
For some reason the below code doesnt produce results as i expect.

#include<stdio. h>

int main()
{

int hi=0xff;
int low=0xee;
long long big ;
big = ((long long) hi ) << 32 | low;

printf("%16x \n ",big);

}

I am lost trying to figure out the mistake i am making.Can you help me
out.Is it compiler dependent?

Deepak
What did you expect? Try this.

#include <stdio.h>

int main(void)
{
int hi = 0xff;
int low = 0xee;
long long big;
big = ((long long)hi) << 32 | low;
printf("%016Lx\ n", big);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 12 '08 #10

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

Similar topics

2
3929
by: Pat | last post by:
Hi, I want to write a int64 data into a file using fprintf(). In the fprintf argument, which one (%d or %u or other) should be used? Thanks. Pat
6
41198
by: ESOJAY | last post by:
Is "typedef long long int64" the best way to define an int64 type in c? Are there better alternatives? Thanks, ESOJAY -- ESOJAY
4
2167
by: Simon Devlin | last post by:
Hi folks, I've been bashing my head against this all afternoon and am now totally baffled. Given this (a simple routine to turn a ip address string into an decimal) <snip> Dim Parts(3) as string ' --> each element is an integer in the range 0-255 Dim Big as int64 = 0
5
5330
by: harishashim | last post by:
I have gone through necessary step and have been able to use a .Net libraries (created using C#) in VB6. It run good untill I try to use certain function in the library that is using Int64 type as parameter. VB6 give the following error Compile Error: Function or interface marked as restricted, or the function use an automation type not supported in Visual Basic.
10
2900
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any other way to get similar effect for Int64 type? Another question less urging question is, why long variables can't be used as volatile? I understand that in 32-bit arch. 64-bit operations are not atomic, but seems that there could be locks or so...
14
9650
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. But I'm not good with the E+??? notation so when I'm told double holds -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.
12
3041
by: Allen | last post by:
My C extension works wrong, and debug it, found that sizeof (INT64) = 4, not 8. I compile on Windows XP platform. Please tell me how to fix it to support INT64? Thanks.
3
1583
by: Abubakar | last post by:
Hi, the project we are working on uses "int" for all integral type data (i have also declared size_t at some places where crt functions return size_t type). Now our application has to deal with files larger than 4gb. Typical places where my code will have to deal with more than 4gb data/figure is calculating the sizes of files and storing them in some place, this can be like 6 or 7 or more gb of figure. Breaking down the 6 or 7 gb archive...
2
2592
by: Peter Larsen [] | last post by:
Hi, How do i combine two 32 bit integers into one int64 in .net/C# ?? BR Peter
3
10764
by: Tim Sprout | last post by:
To convert hex to Int64 I can use: string hex = "413208A97245F5AE"; Int64 intFromHex = System.Convert.ToInt64(hex, 16); //intFromHex = 4697826885160531374 How do I reverse this, and convert Int64 to hex? Thanks,
0
9710
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
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
10593
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...
1
10329
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
9163
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
7626
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
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
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

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.