Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C Bignum Bitshift

Status
Not open for further replies.

crimeanbob

Programmer
Mar 7, 2002
8
US
I have C code to handle a 23 digit number as a 'bignum' by holding the digits in a char array. I also have functions to perform basic arithmetic on the bignum.

My problem is that once I have this number I must perform a bitshift. I need to shift right 16 bits. This is o.k. as I can just divide it by 2^16 but doing that I lose the shifted bits. I need to know these as the right 16 bits and the remaining bits provide the two numbers I need to extract.

Does anyone know of exisiting code to do this or preferably a more elegant solution? My Maths was never my strong point.

Thanks in Advance.
 
The simplest would be to use your "remiander" function logic (if you have one) since the right-shifted bits will be the remained after division by 2^16.

________________________________________
[hippy]Roger J Coult; Grimsby, UK
In the game of life the dice have an odd number of sides.
 
thanks, and that means I can multiply the result by 2^16 and subtract that from the original to get the remainder. I have no remainder functionality but this will do as it doesn't have to be fast.

Thanks for the spark.
 
I wrote a similar set of functions to handle arbitrary length integers. What you might want to do is just grab the last two bytes in your array that get shifted off and move all the other bytes in the array down by two.

Dennis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top