Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:23:46+00:00 2026-05-23T21:23:46+00:00

You have an ASCII string representing a 128-bit unsigned integer number n, i.e. 0

  • 0

You have an ASCII string representing a 128-bit unsigned integer number n, i.e. 0 <= n < 2^128.

Give an algorithm to extract the most significant 32 bits of the binary representation of n and return them as the unsigned 32 bit integer they encode.

What is a fast way to do this, i.e. something better than implementing your own big number division and modulo 2 operations.
Assume a 32bit machine, i. e. you don’t have 64-bit built-in types.

Examples:
For brevity, let’s take 4 bit integers and extract the leading 2 bits:

2(0010) –> 0(00)
7(0111) –> 1(01)
8(1000) –> 2(10)
13(1101) –> 3(11)

This is NOT a homework question. Updating my algo skills for an interview.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:23:47+00:00Added an answer on May 23, 2026 at 9:23 pm

    I don’t see a more efficient way than to simply emulate (a limited form of) 128 bit arithmetic.

    Let’s say we have a function mul10add(a, b) which calculates 10*a + b and returns the lower 32 bits of the answer together with a carry value. If we have 64-bit arithmetic it can be implemented as (in pseudo-code):

    (word32, word32) mul10add(word32 a, word32 b):
        word64 result = 10*a + b
        word32 carry  = result >> 32
        return (result, carry)
    

    Now, we take the normal decimal-to-binary algorithm and represent the 128-bit number n with four 32-bit words x, y, z and w such that n = x*2^96 + y*2^64 + z*2^32 + w. We can then chain calls to mul10add together to perform the equivalent of n = 10*n + digitToInt(decimal[i]).

    word32 high32bits(string decimal):
        word32 x = 0, y = 0, z = 0, w = 0
    
        # carry
        word32 c = 0
    
        for i in 0..length(decimal)-1
           (w, c) = mul10add(w, digitToInt(decimal[i]))
           (z, c) = mul10add(z, c)
           (y, c) = mul10add(y, c)
           (x, _) = mul10add(x, c)
    
        return x
    

    We don’t actually need a 64-bit architecture to implement mul10add, however. On x86 we have the mul instruction which multiplies two 32-bit numbers to get a 64-bit number with the upper 32 bits stored in edx and the lower ones in eax. There’s also the adc instruction, which adds two numbers but includes the carry from a previous add. So, in pseudo-assembly:

    (word32, word32) mul10add(word32 a, word32 b):
        word32 result, carry
        asm:
            mov a, %eax
            mul 10
            add b, %eax
            adc 0, %edx
            mov %eax, result
            mov %edx, carry
        return (result, carry)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a large number of very large ASCII files of numerical data
I have an ASCII String, with HTML entities, like: &agrave; &uml; &ccedil; I need
I have an ASCII string (A null terminated char array) in a console app.
I have a multi-line ASCII string coming from some (Windows/UNIX/...) system. Now, I know
I have tried to convert an ascii string to an escaped pseudo unicode escaped
I have a series of ASCII flat files coming in from a mainframe to
I have to read invoice ascii files that are structured in a really convoluted
I have a scrolling LED sign that takes messages in either ASCII or (using
I have this look up table : char *table[ascii][morse]; where ascii is an int
I have a string encoded by Escape in VBScript: The Escape function returns a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.