473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Understanding problem of long numbers

Hi.
My problem seems to be simple, but I can not figure out a solution.
Problem: I have a number that consists of 128 bit. I want to compute its
modulo (modulo is at the range < 2^16). So that there is no 128 bit
datatype in C++ I just cannot imagine how I can work out qa solution for
that.

greets, Hipo
Jun 15 '06 #1
4 2086
Hipo wrote:
Hi.
My problem seems to be simple, but I can not figure out a solution.
Problem: I have a number that consists of 128 bit. I want to compute its
modulo (modulo is at the range < 2^16). So that there is no 128 bit
datatype in C++ I just cannot imagine how I can work out qa solution for
that.


Basically you need to do a division with remainder. Think about how you
do division with pencil and paper and try to apply this to the problem.
You would need to represent your 128 bit quantity as several smaller
integers that are representable in C++.

Jun 15 '06 #2

Hipo wrote:
Hi.
My problem seems to be simple, but I can not figure out a solution.
Problem: I have a number that consists of 128 bit. I want to compute its
modulo (modulo is at the range < 2^16). So that there is no 128 bit
datatype in C++ I just cannot imagine how I can work out qa solution for
that.

greets, Hipo


Surely there's a big number library that does this? Maybe something
like: http://indigo.ie/~mscott/

I've not used the library, but there's plenty of others around too.

Jun 15 '06 #3
Hipo wrote:
Hi.
My problem seems to be simple, but I can not figure out a solution.
Problem: I have a number that consists of 128 bit. I want to compute its
modulo (modulo is at the range < 2^16). So that there is no 128 bit
datatype in C++ I just cannot imagine how I can work out qa solution for
that.


Long division!

Suppose you only have 32 bit numbers to work with, and your machine is
capable of dividing a 64 bit number by a 32 bit number to yield 32 bit
remainder and quotient. What you can do is pretend that the 128 number
is four digit number in base 4-billion-something, each digit being 32
bits wide.

It's convenient to use hexadecimal for this. For instance consider this
128 bit number as your n:

1ADFCBE3 391AF76B 1FFC0104 EDA9F034

I wrote it as four groups of 32 bits. You can think of these as four
huge digits in a large base, base 2^32. I.e. the number can be written
as the sum of these four numbers

1ADFCBE3 00000000 00000000 00000000

391AF76B 00000000 00000000

1FFC0104 00000000

EDA9F034

Suppose we give the symbolic names A, B, C and D to those four 32 bit
digits. This number n then takes the form A * 2^96 + B * 2^64 + C *
2^32 + D.

We can then just hide all that and write the number in a condensed way
as

A B C D // A B C D are 32 bit chunks

So now you can do long division by M (using 32 bit digits!!!) and find
the remainder:

________
M | A B C D

First find how many times M goes into A, blah blah blah. You know the
routine from grade school, except that the digits go from 0 to 4.7
billion instead of 0 to 9 and the machine does the work.

Let's work through it, using 7D0 hex (2000 decimal) as M.

______________________________________
7D0 |1ADFCBE3 391AF76B 1FFC0104 EDA9F034

How many times does 7D0 go into A? Or rather into 1ADFCBE3? Of
course, we make the computer divide them. And it knows how to do that
because we are dealing only with 32 bit quantities. The answer is
3709D, with a remainder of 153 (all hex). So let's write that in:

____3709D________________________________
7D0 |1ADFCBE3 391AF76B 1FFC0104 EDA9F034
153

So now we combine the 153 remainder with the next 32-bit-digit to make
153391AF76B. We now do 64/32 division to get the next quotient and
remainder, which are 2B6BA956 and 78B, respectively. Put 'em in:

____3709D_2B6BA956_______________________
7D0 |1ADFCBE3 391AF76B 1FFC0104 EDA9F034
153 391AF76B
78B

Hoo wee, this is getting exciting: the number is emerging. We "bring
down" the 1FFC0104, combine it with the remainder to get 78B1FFC0104,
and keep going:

____3709D_2B6BA956_F72F1A1C______________
7D0 |1ADFCBE3 391AF76B 1FFC0104 EDA9F034
153 391AF76B
78B 1FFC0104
644

We are only seconds away from the final answer. Drum roll! Bring it
down, another 64-32 division and remainder and ...

____3709D_2B6BA956_F72F1A1C_CD6E4B00_____
7D0 |1ADFCBE3 391AF76B 1FFC0104 EDA9F034
153 391AF76B
78B 1FFC0104
644 EDA9F034
34

Tada, the remainder is 34, and the quotient is
3709D2B6BA956F72F1A1CCD6E4B00.

If you have a 64 bit type available as an extension in your C++
compiler (the C language has a 64 bit type as of 1999) you can do it
like this. Or you can stick with 32 bit types; the number is broken
into eight 16 bit groups instead.

If you're not interested in the quotient, you just throw it away,
because all you need is the remainder from each division to feed into
the next one.

The most productive solution, however, is to get a bignum library. But
then you would never learn how it's done, right? :)

Jun 15 '06 #4
Great thanks. That helps me a lot.

greets, Hipo
Jun 16 '06 #5

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

Similar topics

83
by: kartik | last post by:
there seems to be a serious problem with allowing numbers to grow in a nearly unbounded manner, as int/long unification does: it hides bugs. most of the time, i expect my numbers to be small. 2**31...
2
by: Dhruva Hein | last post by:
Hi. I am trying to understand a section of code written for Plone and I am having problems understanding the Python syntax in a few of the following lines. I'd be grateful if someone could help...
4
by: Clarence | last post by:
Hi - I have a problem and here is the verbose version of what I am trying to do (better too much info than not enough). I am searching through about 4,700 XML files containing company contact...
10
by: RA Scheltema | last post by:
hi all, A small question about serializing and deserializing a long in a platform independent manner. Can this be done with the following code ?: char buf; long val = 35456;
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
11
by: bogusexception | last post by:
(or.. "I'm getting too much Tails and not enough Heads") I'm running into a very strange problem with random numbers and long numbers. To demonstrate the problem, I've created a simple test....
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
4
by: pranav | last post by:
Hello, I want to read a BMP file, do some processing and then write it in a new file. The problem is in the third step. For reading the file, i have converted the file into decimal numbers,...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.