LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 04-08-2013, 09:33 PM   #1
bimboleum
Member
 
Registered: Oct 2009
Posts: 46

Rep: Reputation: 9
strtoull, 64bit arithmetic


Hey,
I have a programming problem that is driving me nuts .. and I suspect it has a very simple solution. I am asking here because my system is a Slackware14 64-bit system but if another forum would be more suitable please say so.

I want to perform 64-bit arithmetiuc AND string conversions using unsigned 64 bit entities. My problem is that I appear to be doing 32-bit arithmetic.

I have a test program thus:-

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, char ** argv)
{
    unsigned long long x,y;
    unsigned long long z;
    char *a = "4294967294";
    char *b = "4294967295";
    char *c = "4294967296"; /* this is 4*1024*1024*1024 */
    char *d = "4294967297";
    char *e = "0x0000000101234567";

    x = strtoull(a,NULL,0);
    printf("string %s converts to 0x%16.16X\n",a,x);
    y = strtoull(b,NULL,0);
    printf("string %s converts to 0x%16.16X\n",b,y);
    z = (unsigned long long)x + (unsigned long long)y;
    printf("sum is 0x%16.16X\n",z);
    x = strtoull(c,NULL,0);
    printf("string %s converts to 0x%16.16X\n",c,x);
    x = strtoull(d,NULL,0);
    printf("string %s converts to 0x%16.16X\n",d,x);
    x = strtoull(e,NULL,0);
    printf("string %s converts to 0x%16.16X\n",e,x);
    return(0);
};
This program was compiled using:-
cc -o tt tt.c

and the compiled progam gives results:-

Code:
=>./tt 
string 4294967294 converts to 0x00000000FFFFFFFE
string 4294967295 converts to 0x00000000FFFFFFFF
sum is 0x00000000FFFFFFFD
string 4294967296 converts to 0x0000000000000000
string 4294967297 converts to 0x0000000000000001
string 0x0000000101234567 converts to 0x0000000001234567
As you can see, all of the conversions are truncated to 32 bits and the 64-bit addition is handled as a signed 32-bit addition.

Can anyone tell me what I am doing wrong ... please .. this is driving me nuts

cheers
pete

pete hilton
saruman@ruvolo-hilton.org
 
Old 04-08-2013, 09:52 PM   #2
D1ver
Member
 
Registered: Jan 2010
Distribution: Slackware 13.37
Posts: 527
Blog Entries: 3

Rep: Reputation: 126Reputation: 126
I think the problem is your printf formatting codes.

You want to change %16.16X to %16.16LLX, as they're long long hex numbers..

Try compiling with the -Wall switch to increase the error/warning level..

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, char ** argv)
{
    unsigned long long x,y;
    unsigned long long z;
    char *a = "4294967294";
    char *b = "4294967295";
    char *c = "4294967296"; /* this is 4*1024*1024*1024 */
    char *d = "4294967297";
    char *e = "0x0000000101234567";

    x = strtoull(a,NULL,0);
    printf("string %s converts to 0x%16.16llX\n",a,x);
    y = strtoull(b,NULL,0);
    printf("string %s converts to 0x%16.16llX\n",b,y);
    z = (unsigned long long)x + (unsigned long long)y;
    printf("sum is 0x%16.16llX\n",z);
    x = strtoull(c,NULL,0);
    printf("string %s converts to 0x%16.16llX\n",c,x);
    x = strtoull(d,NULL,0);
    printf("string %s converts to 0x%16.16llX\n",d,x);
    x = strtoull(e,NULL,0);
    printf("string %s converts to 0x%16.16llX\n",e,x);
    return(0);
};
Gives

Code:
neil@satellite:~/Documents/Code$ ./mathtest 
string 4294967294 converts to 0x00000000FFFFFFFE
string 4294967295 converts to 0x00000000FFFFFFFF
sum is 0x00000001FFFFFFFD
string 4294967296 converts to 0x0000000100000000
string 4294967297 converts to 0x0000000100000001
string 0x0000000101234567 converts to 0x0000000101234567

Last edited by D1ver; 04-08-2013 at 09:54 PM. Reason: double post :s
 
Old 04-08-2013, 11:01 PM   #3
bimboleum
Member
 
Registered: Oct 2009
Posts: 46

Original Poster
Rep: Reputation: 9
Hey,
You guys are just magic.

I KNEW I was doing something stupid.

Many thanks

cheers
pete

pete hilton

saruman@ruvolo-hilton.org
 
Old 04-09-2013, 09:03 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
There is no real difference between unsigned long and unsigned long long on a 64-bit machine.

Run this and see:
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
		printf("int=%lu\n",sizeof(int));
		printf("long=%lu\n",sizeof(long));
		printf("ulint=%lu\n",sizeof(unsigned int));
		printf("ulong=%lu\n",sizeof(unsigned long));
		printf("ullong=%lu\n",sizeof(unsigned long long));

	return 0;
}
Code:
int=4
long=8
ulint=4
ulong=8
ullong=8
I wrote a program to convert strings to octal and hex and back, it may interest you:

Code:
// convert between unsigned long, octal, hex
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
	// init
	unsigned long ul = 0;
	
	// parse argc
	if (4 != argc)
	{
		printf("Usage:\n%s [u|o|x|X](input type) [u|o|x|X](output type) input\nExample:\n%s u x 123456\n", argv[0], argv[0]);
		exit(1);
	}
	
	// input
	if (!strcmp(argv[1],"u"))
	{
		sscanf(argv[3], "%lu", &ul);
	}
	else if (!strcmp(argv[1],"o"))
	{
		sscanf(argv[3], "%lo", &ul);
	}
	else if (!strcmp(argv[1],"x"))
	{
		sscanf(argv[3], "%lx", &ul);
	}
	else if (!strcmp(argv[1],"X"))
	{
		sscanf(argv[3], "%lX", &ul);
	}
	
	// output
	if (!strcmp(argv[2],"u"))
	{
		printf("%lu\n", ul);
	}
	else if (!strcmp(argv[2],"o"))
	{
		printf("%lo\n", ul);
	}
	else if (!strcmp(argv[2],"x"))
	{
		printf("%lx\n", ul);
	}
	else if (!strcmp(argv[2],"X"))
	{
		printf("%lX\n", ul);
	}
	
	return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fedora 64bit and Ubuntu 12.04 64bit sees only 7.8GiB when i have 8GB. no igp. snowcake Linux - Hardware 11 11-04-2012 01:48 AM
[SOLVED] Which 64bit Debian Wheezy Release is wright for my Intel 64bit P8700 CPU PC? esgol Linux - Newbie 15 07-15-2012 04:50 PM
Which 64bit Debian wheezy Release is wright for my Intel 64bit P8700 CPU? esgol Linux - Newbie 1 07-15-2012 04:11 AM
64bit RPM installation is failing on SLES-10/11 but working fine on RHEL4/5 64bit os raghuhb Linux - Software 2 11-06-2009 08:31 AM
Can't install amarok 64bit du eto missing provider of libpq.so.4()(64bit) Ossah Linux - Software 1 04-21-2007 10:23 PM


All times are GMT -5. The time now is 10:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration