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 6623145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:29:54+00:00 2026-05-25T21:29:54+00:00

First off, lets assume I have endian sorted out and know the size of

  • 0

First off, lets assume I have endian sorted out and know the size of the types a priori.

Here’s what I am doing: I have a function bs2i(char *bitStr) that takes in a character array representing the binary of a signed char/short/int/long. The array can be of any length <= sizeof(long long).

What I want to do is take bitStr[0] and set it as the sign bit such that it won’t be truncated if I do something like char val = bs2i("111"). In this case, the function should return -125, as the bits would be set as “000…00010000011.” To do this, I need to know how long the final type needs to be so I can shift the msb to the proper spot.

This is what I have thus far:

size_t size = (((strlen(bitStr)-1)>>3)+1)<<3;

But I just realized that only works for chars and shorts. For instance, a string of length 17 should return size 32, but would only return 24 with this method. Is there an efficient way to determine what the proper length should be?

For reference, here is the full function:

long long bs2i(char *bitStr) {
    long long val = 0l;
    size_t len = (((strlen(bitStr) - 1) >> 3) + 1) << 3;
    char msb = *bitStr & 1;
    bitStr++;
    while (*bitStr) {
        val = (val << 1l) | (*bitStr & 1);
        bitStr++;
    }
    val = (msb << len-1l) | val; /* Use MSB as sign bit */

    return val;
}

Bonus points, I guess, if there’s a good way to do this that doesn’t require a priori knowledge of type sizes.

  • 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-25T21:29:54+00:00Added an answer on May 25, 2026 at 9:29 pm

    It sounds like you want to find the smallest power-of-two, multiple of 8 number of bits that would fit as many bits as there are characters in the string. If you have a maximum of 64 bits, why not just use a switch on the string length?

    switch((strlen(bitStr) - 1) >> 3) {
      case 0: return 8;  /* strlen <= 8 */
      case 1: return 16; /* strlen <= 16 */
      case 2:
      case 3: return 32; /* strlen <= 32 */
      case 4:
      case 5:
      case 6:
      case 7: return 64; /* strlen <= 64 */
      default: return 0;
    }
    

    Alternatively, you could try this:

    int reqIntSz(char *bitStr)
    {
      int len = strlen(bitStr);
      int res = 8;
      while ( len > res ) res <<= 1;
      return res;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me clarify the platforms we are using. We have an ASP.NET
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
First off, I'm aware this is a bad practice and I have answered many
so lets layout the design first. I have a combobox with a button, and
hello im just having a problem here with PREG_MATCH_ALL() lets say i have 2
First off, let me define the end goal: I'd like to Wordpress (version 2.8)
First off, I'm a Paradox newbie. Secondly, I'm querying a database of a third-party
First off, let me start by saying that I am totally new to working
First off, I am not a DBA, but I do work in an environment
First off the html row looks like this: <tr class=evenColor> blahblah TheTextIneed blahblah and

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.