0

I can only use bitwise operations and pointer arithmetic to solve this problem. I am converting from binary to unsigned int.

The function I am writing is:

unsigned int atob(const char* nptr);

atob("101") should return 5, atob("11000") should return 24, atob("11$") should return 3, and atop("") should return 0.

I'm pretty new to bitwise operations, so I really need some help specifically in that area.

edit:

nptr can only be incremented, not other inc/dec's are allowed.

4
  • 3
    What have you tried? And is this homework? Are you not allowed to use comparisons? Commented Jul 3, 2012 at 12:21
  • How would you solve it if you were allowed to use * and +? (Hint: *2 === <<1, + can here be done with |.) Commented Jul 3, 2012 at 12:25
  • This is not homework. Just studying material. I have no problem at all converting from binary to decimal, but I am not coherent on how to do it with bitwise operations because I rarely use them for anything. Commented Jul 3, 2012 at 12:27
  • atob("101") should return 3 I think you meant 5? Commented Jul 3, 2012 at 12:28

3 Answers 3

2
unsigned bits2val(char *bits)
{
    unsigned val;

    for (val = 0; *bits; bits++) {
        if (*bits == '1') 
            val = (val << 1) | 1;
        else if (*bits == '0' ) 
            val <<= 1;
        else 
            break;
    }

    return val;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Very difficult to read, do you mind cleaning up the post a bit?
Your code formatting is a bit off of the norm, I took the liberty of normalizing it.
Ah, you are discussing whitespace. I never use extra lines for single statements. (I still find mine easier to read. The extra lines and braces only distract, IMHO) BTW: by correcting my style you are takng the chance from people to be confronted with different styles.
Wildplasser, that is true, but here on SO, the style that I have there is the most widely used one. Feel free to use whichever you want, just make sure that your indentation is proper. (Also, I did give you a +1).
I hate corporate styles. Even more in non-corporate environments. Please learn how to cope with diversity, instead of imposing your views on others. Thank you.
|
1

Here's my example implementation, using just shifts and ors (assuming you can use ++ for string-manipulation):

unsigned atob(const char *input)
{
    unsigned result = 0;
    unsigned currentBit = 0;

    // we need to go right to left;
    const char *end = input;
    // make sure we only read '0's and '1's
    while ((*end == '0') || (*end == '1'))
    {
        end++;
    }

    while (--end >= input) {
        // check for overflow
        if ((currentBit >> 3) > sizeof(result))
            break;

        char isCurrentBitSet = *end == '1';
        unsigned setValue = (isCurrentBitSet << currentBit);
        result |= setValue;

        currentBit++;
    }

    return result;
}

4 Comments

Looks good except I cannot use division! That is why I am struggling.
@JordanCarney that's only to check for overflow. If you know the size of unsigned at compile time, it's a non-issue.
OK, I understand. I actually did not need to check for overflow in this exercise, so that is okay then!
One more question: Is it possible to do this by only incrementing const char* input and no other inc/dec?
0

Start with the basics http://www.wikihow.com/Convert-from-Decimal-to-Binary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.