Click here to Skip to main content
16,009,477 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: INTEGER CALCULATION Pin
Rane2-Sep-08 1:13
Rane2-Sep-08 1:13 
GeneralRe: INTEGER CALCULATION Pin
manoj_362-Sep-08 7:49
manoj_362-Sep-08 7:49 
GeneralRe: INTEGER CALCULATION Pin
David Crow2-Sep-08 7:51
David Crow2-Sep-08 7:51 
GeneralRe: INTEGER CALCULATION Pin
Mark Salsbery2-Sep-08 8:06
Mark Salsbery2-Sep-08 8:06 
GeneralRe: INTEGER CALCULATION Pin
manoj_364-Sep-08 17:44
manoj_364-Sep-08 17:44 
GeneralRe: INTEGER CALCULATION Pin
Mark Salsbery5-Sep-08 4:58
Mark Salsbery5-Sep-08 4:58 
GeneralRe: INTEGER CALCULATION Pin
manoj_365-Sep-08 5:48
manoj_365-Sep-08 5:48 
AnswerRe: INTEGER CALCULATION Pin
Member 419459315-Sep-08 13:05
Member 419459315-Sep-08 13:05 
manoj_36 wrote:
I am doing a calculation, in which i have to calculate the power of say(for e.g 2 ^ 100 , 2 ^150 etc) .
And i have to see if that 2^100 has a pattern of 777 in it.



Let me start with a word of warning about the size of numbers you will be working with. You have not stated the upper limit of the power of 2 you are working with, but if you use, for example, 2 ^ 65536, this will give you a number of 2.x...e+19728 (try this with calculator using 2 x^y 65536). That means that the decimal number to represent this value will have 19729 digits. This number in binary will have 65536 bits, or 8192 bytes, or 2048 unsigned integers.

Setting the value of a power of 2 is trivial. Start by defining an array of unsigned integers which can contain the largest binary number you expect (plus several more - I'll explain later). Do not try to create 2 ^ 100 by multiplying 2 by 2 100 times, use math. A power of 2 in binary is nothing but a single bit positioned in the binary number, starting with the least significant bit for 2 ^ 0, the next most significant bit for 2 ^ 1, etc. To multiply two powers of 2, just add their exponents, e.g. (2 ^ 10) * (2 ^ 15) = 2 ^ 25. To express the binary value of a power of 2, start by zeroing the array of unsigned integers, then divide the power of 2 by 32, and also save the remainder (the mod 32). Set an unsigned integer to a value of 1, then left shift it by the modulo value (0 to 31 places), then save it in the array at the index of the quotient of the division.

As can be seen from the above, it is trivial to create such a binary value, but now, how to express it as a decimal value so you can scan the result for a string of "777" (remember the bit about how big this string could get). Also take into account array indexing - are you indexing from 0 to n-1 or indexing from 1 to n.

To convert this number to decimal, start from the top end of the array (the most significant end) which should contain a couple of zero values at the top. The reason for these extra zero values is that the division process will not use up each unsigned integer, and the remainder creeps up. When dividing a binary number by a binary number, the number of bits of the resulting quotient will equal the difference between the number of bits in the dividend less the number of bits in the divisor. We will divide this huge binary number (thousands of unsigned integers) by a large unsigned integer, namely 10 ^ 9 (the largest power of 10 that will fit in an unsigned integer).
This will result in a quotient that is one unsigned integer less than the dividend, and a remainder that is less than 10 ^ 9. Save this remainder in another array of unsigned integers. Repeat the process, dividing the new quotient by 10 ^ 9 to create a shorter quotient and a new remainder, saving the remainders in the new array. Continue this process until the resultant quotient is 0 and the last remainder has been saved. Remember the bit about 19729 digits, well, 19729 / 9 = 2192 so the remainder array will have to be at least that long.

Now to create the decimal number. Access each of the saved remainders, starting with the first one saved. This remainder contains the least significant 9 digits of the decimal number, so you will start saving the decimal number in reverse, starting at character (count of remainders * 9). Actually, save a terminating null character 1 character past that, then start the conversion. Divide the remainder 9 times by a value of 10, saving the quotient for each division, and using the remainder for each division to access a decimal character in a string of "0123456789". Save each character in the target decimal string, each character in front of the last saved character (the first in front of the terminating null). Repeat the process for each of the remainders. There will potentially be leading zero characters at the head of the string which can be ignored for your pattern search application (skip by the leading zeros if you intend to print out this string, but remember that 19728 characters takes 247 lines of 80 characters to print - probably not what you want to do).

I'm not sure what your coding looks like and whether or not you can escape to assembly to do the division calculation, but escaping to assembly sure will speed up the entire process rather than trying to cast a moving indexed value as a unsigned long integer. I can help with this if using assembly is possible. Note: I know C, some C++, but really am a MASM coder.

Dave.
GeneralRe: INTEGER CALCULATION Pin
manoj_3614-Sep-08 19:33
manoj_3614-Sep-08 19:33 
Questionproblem in acessing bluetooth dongle from c++ problem Pin
manju23reddy2-Sep-08 1:01
manju23reddy2-Sep-08 1:01 
AnswerRe: problem in acessing bluetooth dongle from c++ problem Pin
auralius manurung2-Sep-08 16:13
auralius manurung2-Sep-08 16:13 
QuestionApplication stopped working automaticaly. Pin
Le@rner2-Sep-08 0:52
Le@rner2-Sep-08 0:52 
AnswerRe: Application stopped working automaticaly. Pin
Rane2-Sep-08 1:19
Rane2-Sep-08 1:19 
AnswerRe: Application stopped working automaticaly. Pin
Jijo.Raj2-Sep-08 4:15
Jijo.Raj2-Sep-08 4:15 
QuestionUser Account Control(UAC) of Vista takes effect on application. Pin
Le@rner2-Sep-08 0:44
Le@rner2-Sep-08 0:44 
AnswerRe: User Account Control(UAC) of Vista takes effect on application. Pin
Rajesh R Subramanian2-Sep-08 2:00
professionalRajesh R Subramanian2-Sep-08 2:00 
QuestionDeleted question about advanced debugging? Pin
Rostfrei2-Sep-08 0:23
Rostfrei2-Sep-08 0:23 
AnswerRe: Deleted question about advanced debugging? Pin
super_ttd2-Sep-08 0:29
super_ttd2-Sep-08 0:29 
AnswerRe: Deleted question about advanced debugging? Pin
Rajesh R Subramanian2-Sep-08 0:31
professionalRajesh R Subramanian2-Sep-08 0:31 
GeneralRe: Deleted question about advanced debugging? Pin
Rostfrei2-Sep-08 0:37
Rostfrei2-Sep-08 0:37 
GeneralRe: Deleted question about advanced debugging? Pin
kakan2-Sep-08 0:50
professionalkakan2-Sep-08 0:50 
GeneralRe: Deleted question about advanced debugging? Pin
Rostfrei2-Sep-08 0:56
Rostfrei2-Sep-08 0:56 
GeneralRe: Deleted question about advanced debugging? Pin
Rajesh R Subramanian2-Sep-08 1:05
professionalRajesh R Subramanian2-Sep-08 1:05 
QuestionGetting the Parent of an item in Tree control in win32 Pin
gayatri.neelema2-Sep-08 0:14
gayatri.neelema2-Sep-08 0:14 
AnswerRe: Getting the Parent of an item in Tree control in win32 Pin
Mark Salsbery2-Sep-08 7:34
Mark Salsbery2-Sep-08 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.