473,785 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dealing with a large integer

Hi all

I have an large integer in this format
x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
now I must convert it to this format
y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5

x1-x5 is given.I must get y1-y4 from it.

How can I do this on my 32bit PC?

Sorry for that my English is poor.

Thanks.

Mar 3 '06
42 3377
I rewrite my code but the uint64_t seems can't work well.

The code is here:
=============== =========
#include <stdio.h>
#include <stdint.h>
#include "compress.h "

int main()
{
struct code6 mycode6;
struct code5 mycode5;

mycode6.item1=1 ;
mycode6.item2=2 ;
mycode6.item3=3 ;
mycode6.item4=4 ;
mycode6.item5=5 ;
mycode6.item6=6 ;

mycode5=codecom press(mycode6);

printf("mycode5 .item1 is: %d\n",mycode5.i tem1);
printf("mycode5 .item2 is: %d\n",mycode5.i tem2);
printf("mycode5 .item3 is: %d\n",mycode5.i tem3);
printf("mycode5 .item4 is: %d\n",mycode5.i tem4);
printf("mycode5 .item5 is: %d\n",mycode5.i tem5);

return 0;
}
=============== =========

and the compress.h is here:

=============== =========
#include <stdint.h>

struct code6
{
unsigned long item1;
unsigned long item2;
unsigned long item3;
unsigned long item4;
unsigned long item5;
unsigned long item6;
};

struct code5
{
unsigned long item1;
unsigned long item2;
unsigned long item3;
unsigned long item4;
unsigned long item5;
};

struct code5 codecompress(st ruct code6 origcode)
{
struct code5 ret;
uint64_t math64;

math64=origcode .item1*(256^5)+ origcode.item2* (256^4)+origcod e.item3*(256^3) +origcode.item4 *(256^2)+origco de.item5*256+or igcode.item6;
printf("math64 is: %X\n",math64);

ret.item5=(math 64)%(uint64_t)9 00;
math64/=900;
ret.item4=(math 64)%900;
math64/=900;
ret.item3=(math 64)%900;
math64/=900;
ret.item2=(math 64)%900;
math64/=900;
ret.item1=(math 64)%900;

return ret;
}

=============== =========

After the code runs it displays that

------------------------
math64 is: F24
mycode5.item1 is: 0
mycode5.item2 is: 0
mycode5.item3 is: 0
mycode5.item4 is: 4
mycode5.item5 is: 276
------------------------

That's incorret.Is there something wrong with my code?

Thanks.
Mar 5 '06 #21
In article <du**********@n ews.yaako.com>,
yong <lu********@eno rth.com.cn> wrote:
I rewrite my code but the uint64_t seems can't work well.
Unfortunately you did not quote any of the original task.

The code is here: math64=origcode .item1*(256^5)+ origcode.item2* (256^4)+origcod e.item3*(256^3) +origcode.item4 *(256^2)+origco de.item5*256+or igcode.item6;


In C, the ^ operator is "Exclusive OR". In your description of
the original task, I would be surprised if your use of ^
did not indicate exponentiation there.

In C, 256 to the power
of 0 is 1
of 1 is 1<<8
of 2 is 1<<16
of 3 is 1<<24
of 4 is 1<<32
of 5 is 1<<40

On most systems, 1<<32 is bigger than an unsigned long will hold.
C89 does not promise that anything from 1<<32 upwards is available.
C99 does provide unsigned long long for that purpose, but you
must be careful to avoid accidently working in narrower types
in an intermediate stage.

You do not actually need to use the multiplications or additions
in your calculation of math64. Presuming C99, your code could instead be

math64 = (unsigned long long) origcode.item1 << 40 |
(unsigned long long) origcode.item2 << 32 |
(unsigned long long) origcode.item3 << 24 |
(unsigned long long) origcode.item4 << 16 |
(unsigned long long) origcode.item5 << 8 |
(unsigned long long) origcode.item6;

--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Mar 5 '06 #22
Walter Roberson wrote:
In article <du**********@n ews.yaako.com>,
yong <lu********@eno rth.com.cn> wrote:
I rewrite my code but the uint64_t seems can't work well.

Unfortunately you did not quote any of the original task.
The code is here:


math64=origcode .item1*(256^5)+ origcode.item2* (256^4)+origcod e.item3*(256^3) +origcode.item4 *(256^2)+origco de.item5*256+or igcode.item6;

In C, the ^ operator is "Exclusive OR". In your description of
the original task, I would be surprised if your use of ^
did not indicate exponentiation there.

In C, 256 to the power
of 0 is 1
of 1 is 1<<8
of 2 is 1<<16
of 3 is 1<<24
of 4 is 1<<32
of 5 is 1<<40

On most systems, 1<<32 is bigger than an unsigned long will hold.
C89 does not promise that anything from 1<<32 upwards is available.
C99 does provide unsigned long long for that purpose, but you
must be careful to avoid accidently working in narrower types
in an intermediate stage.

You do not actually need to use the multiplications or additions
in your calculation of math64. Presuming C99, your code could instead be

math64 = (unsigned long long) origcode.item1 << 40 |
(unsigned long long) origcode.item2 << 32 |
(unsigned long long) origcode.item3 << 24 |
(unsigned long long) origcode.item4 << 16 |
(unsigned long long) origcode.item5 << 8 |
(unsigned long long) origcode.item6;


I rewrite my code and it seems run correctly now.

main.c
=============== ====
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "compress.h "

int main()
{
struct code6 mycode6;
struct code5 mycode5;

mycode6.item1=1 ;
mycode6.item2=2 ;
mycode6.item3=3 ;
mycode6.item4=4 ;
mycode6.item5=5 ;
mycode6.item6=6 ;

mycode5=codecom press(mycode6);

printf("mycode5 .item1 is: %d\n",mycode5.i tem1);
printf("mycode5 .item2 is: %d\n",mycode5.i tem2);
printf("mycode5 .item3 is: %d\n",mycode5.i tem3);
printf("mycode5 .item4 is: %d\n",mycode5.i tem4);
printf("mycode5 .item5 is: %d\n",mycode5.i tem5);

return 0;
}
=============== ====

compress.h
=============== ====
#include <stdint.h>

struct code6
{
unsigned long item1;
unsigned long item2;
unsigned long item3;
unsigned long item4;
unsigned long item5;
unsigned long item6;
};

struct code5
{
unsigned long item1;
unsigned long item2;
unsigned long item3;
unsigned long item4;
unsigned long item5;
};

struct code5 codecompress(st ruct code6 origcode)
{
struct code5 ret;
uint64_t math64;

math64=origcode .item1*((uint64 _t)pow(256,5))+ origcode.item2* ((uint64_t)pow( 256,4))+origcod e.item3*((uint6 4_t)pow(256,3)) +origcode.item4 *((uint64_t)pow (256,2))+origco de.item5*256+or igcode.item6;

ret.item5=(math 64)%(uint64_t)9 00;
math64/=900;
ret.item4=(math 64)%900;
math64/=900;
ret.item3=(math 64)%900;
math64/=900;
ret.item2=(math 64)%900;
math64/=900;
ret.item1=(math 64)%900;

return ret;
}
=============== ====
Thank you all. :]

--
My Personal Weblog:
http://spaces.msn.com/cyberisblue
Mar 5 '06 #23

CBFalconer wrote:
Default User wrote:
yong wrote:
I'm using linux.And I found another variable type named uint64_t
in stdint.h which represents a 64bit integer.It's seems not
standard but very useful.


Actually, that is standard as of the latest standard. From the C99
draft standard:

7.18.1.1 Exact-width integer types

[#2] The typedef name uintN_t designates an unsigned integer
type with width N. Thus, uint24_t denotes an unsigned
integer type with a width of exactly 24 bits.

An implementation is not required to provide them.


Actually, under C99, if the underlying hardware has such objects
the implementation must then support them. Otherwise they can be
omitted.


If you read the relevant paragraph in the standard,
you'll see that it doesn't say anything about the
underlying hardware. It's perfectly legal for an
implementation to have 8 bit chars, 16 bit shorts,
and 64 bit ints, longs and long longs, and not define
int32_t, regardless of what hardware it's running on.

Mar 5 '06 #24
On 2006-03-05, en******@yahoo. com <en******@yahoo .com> wrote:

CBFalconer wrote:
Default User wrote:
> yong wrote:
>
>> I'm using linux.And I found another variable type named uint64_t
>> in stdint.h which represents a 64bit integer.It's seems not
>> standard but very useful.
>
> Actually, that is standard as of the latest standard. From the C99
> draft standard:
>
> 7.18.1.1 Exact-width integer types
>
> [#2] The typedef name uintN_t designates an unsigned integer
> type with width N. Thus, uint24_t denotes an unsigned
> integer type with a width of exactly 24 bits.
>
> An implementation is not required to provide them.


Actually, under C99, if the underlying hardware has such objects
the implementation must then support them. Otherwise they can be
omitted.


If you read the relevant paragraph in the standard,
you'll see that it doesn't say anything about the
underlying hardware. It's perfectly legal for an
implementation to have 8 bit chars, 16 bit shorts,
and 64 bit ints, longs and long longs, and not define
int32_t, regardless of what hardware it's running on.


I think his claim is that the types have to be available if the relevant
standard types are twos-complement and have no padding. I don't know if
that's true or not. "hardware" is just a shorthand for "whatever the
'native' types are implemented on."

*wonders why int24_t isn't required if there is a type with 24 bits, no
padding bits, and twos complement representation*
Mar 5 '06 #25
Me
> I have an large integer in this format
x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
now I must convert it to this format
y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5

x1-x5 is given.I must get y1-y4 from it.

How can I do this on my 32bit PC?


It looks like you're trying to convert a base-256 number to a base-900
number (i.e. all the the xN's are in the range [0, 255]). If this is
the case, then you can do this:

unsigned long long x = x1;
x = x<<8 | x2;
x = x<<8 | x3;
x = x<<8 | x4;
x = x<<8 | x5;

And then to get y1..y5:

y5 = x % 900; x /= 900;
y4 = x % 900; x /= 900;
y3 = x % 900; x /= 900;
y2 = x % 900; x /= 900;
y1 = x;

Mar 6 '06 #26
yong <yo************ ****@gmail.com> wrote:
Hi all
I have an large integer in this format [lemm call this X:] x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6
now I must convert it to this format [Y:] y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5
x1-x5 is given.I must get y1-y4 from it.
How can I do this on my 32bit PC?
Sorry for that my English is poor.
Thanks.

Some kind of pdf encoder, eh?
There's probably a lot of literature on smart tricks, but
the basic idea (beside simply using 64-bit integers and brute force --
BTW even 32-bit PC's can do h/w 64-bit arith these days) is to note that

X % 900 = Y % 900 = y4.

Then we see that
256^5 % 900 = 376
256^4 % 900 = 796
256^3 % 900 = 316
256^2 % 900 = 736
256^1 % 900 = 256

So we must have y4 = (376*x1 + 796*x2 + 316*x3 + 736*x4 + 256*x5 + x6) % 900 .
(Look, maw, only 16-bit arithmetic!)

Similiarly for the other y_i.
Mar 6 '06 #27

Jordan Abel wrote:
it is possible for a program to know how long the resulting string from
a sprintf statement will be. if that was a dig at the recent gets
debate, keep in mind it is NOT possible for a program to know how much
the user will type.


Sigh. It sounds like we need to beat the dead horse one more time.

The gets() which I defended gots'ed a string which my own software
had printf'ed. Got it?

James

Mar 6 '06 #28
On 2006-03-06, James Dow Allen <jd*********@ya hoo.com> wrote:

Jordan Abel wrote:
it is possible for a program to know how long the resulting string from
a sprintf statement will be. if that was a dig at the recent gets
debate, keep in mind it is NOT possible for a program to know how much
the user will type.


Sigh. It sounds like we need to beat the dead horse one more time.

The gets() which I defended gots'ed a string which my own software
had printf'ed. Got it?


How did you guarantee this? There is no mechanism in the standard to
ensure that this happens. If gets is removed from the standard, systems
which _do_ provide such a mechanism may provide gets as an extension.
Mar 6 '06 #29

Jordan Abel wrote:
On 2006-03-05, en******@yahoo. com <en******@yahoo .com> wrote:

CBFalconer wrote:
Default User wrote:
> yong wrote:
>
>> I'm using linux.And I found another variable type named uint64_t
>> in stdint.h which represents a 64bit integer.It's seems not
>> standard but very useful.
>
> Actually, that is standard as of the latest standard. From the C99
> draft standard:
>
> 7.18.1.1 Exact-width integer types
>
> [#2] The typedef name uintN_t designates an unsigned integer
> type with width N. Thus, uint24_t denotes an unsigned
> integer type with a width of exactly 24 bits.
>
> An implementation is not required to provide them.

Actually, under C99, if the underlying hardware has such objects
the implementation must then support them. Otherwise they can be
omitted.


If you read the relevant paragraph in the standard,
you'll see that it doesn't say anything about the
underlying hardware. It's perfectly legal for an
implementation to have 8 bit chars, 16 bit shorts,
and 64 bit ints, longs and long longs, and not define
int32_t, regardless of what hardware it's running on.


I think his claim is that the types have to be available if the relevant
standard types are twos-complement and have no padding. I don't know if
that's true or not. "hardware" is just a shorthand for "whatever the
'native' types are implemented on."


If that's the claim that was intended then someone needs
a lesson in writing for clarity.

I find it more likely that your reading is simply overly
charitable. There was no mention, for example, of twos
complement in the original message.

Mar 6 '06 #30

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

Similar topics

8
3271
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
18
5643
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type as u_int64_t and long int etc. My power function is as follows: for(i=0; i<5000; i++) result *= 2;
5
1563
by: platinumbay | last post by:
I am trying to load large flat files; > 250 MB. What is the best practice for this? I have already read through everything I could find on Google, to no avail. I have a SQL DTS process that will load the file in about 7 minutes. My code takes about an hour. Here is a snippet: Dim objSR As StreamReader = System.IO.File.OpenText(objOrigFile)
3
2366
by: sebastian.harko | last post by:
Helllo, What's the general accepted strategy for dealing with very large binary files in C# ? I have to do a program that reads some "multi frame bitmap " files which can reach up to one hundred megs so I need to know how to optimize reading a file.. Best regards, Seb
1
1642
by: Jonathan Wilson | last post by:
I am working on some software which has to deal with data that could be as large as 500mb or so. Currently I am using new and delete to manage this memory but I find it is not ideal and sometimes gives out of memory errors if I open one large data item then free that data item then another large data item even though I have enough memory (including 2GB of physical RAM and 100GB of free disk space for swap file). Are there any functions...
26
3710
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
0
22476
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with a normal integer. I needed this in a C++ program running on a 32-bit UNIX platform. The problem was that the number was 28 digits long and no native datatype in c++ could store a number of that size. (Eg: 1088263455689473669888943602 % 380) Not...
4
5970
by: Shisou | last post by:
hello everyone, Well I tried to solve this one on my own but it seems i need your help again :\ I'm trying to write a program to add or subtract two large integers... easy enough.. Where i run into problems is working with pointers. Here's where i'm at right now. I have a function that takes in the integers as strings and puts it into a struct
16
2045
by: pereges | last post by:
ok so i have written a program in C where I am dealing with huge data(millions and lots of iterations involved) and for some reason the screen tends to freeze and I get no output every time I execute it. However, I have tried to reduce the amount of data and the program runs fine. What could possibly be done to resolve this ?
0
9645
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
10336
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...
0
10155
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...
0
9953
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
8978
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
7502
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.