Question

I have a binary representation of a number as a char array which is at most 18 bits long. I need to store the three bits on the right, and the remaining bits into two different strings using bitwise operators.

For example, I have a char array containing "00000000011101". I need a function that will store "101" (the last three bits) and store "00000000011" (the remaining bits).

However, this must be done using bitwise operators. So I understand that I need to convert the strings to int, and then shift but I'm not sure how to go about this.

Your help would be very much appreciated.

Was it helpful?

Solution

Converting the string to int is easy: start with a zero int, then go through the characters of the binary representation, and check if it is zero or one. First, shift the partial result to the left. Then, if the digit is one, OR the result with 1. Continue to the next character. When you see null terminator '\0', stop.

Here is some pseudocode:

res = 0
ptr = "00000000011101"
while ptr is not pointing to '\0'
    res <<= 1;
    if ptr points to '1'
        res |= 1;

Once you have your int, getting the last three bits is equivalent to AND-ing & the result with seven (its binary representation is 0000111). Getting the rest of the bits is equivalent to shifting >> the number right by three.

Here is some pseudocode:

lastThree = res & 7
topBits = res >> 3

OTHER TIPS

You can always do it yourself. It's actually pretty simple.

int BitwiseOr(char str1[], char str2[]) //Although it probably works on not whole numbers too.
{
    int i, output = 0;
    char* outputstr = malloc(sizeof(char)(strlen(str1)>strlen(str2)?strlen(str1):strlen(str2))); //Although size of char is mostly 1. It's defined to fit to the biggest string size (binary representation there is).
    char* rstr1;
    char* rstr2;
    if(strlen(str1)==strlen(str2)
    {
        rstr1 = str1; //Point to the same address.
        rstr2 = str2; //As we are going to use these variables for cases if the strings' length aren't equal and we need to add zeros to the left.
    }
    else if(strlen(str1)>strlen(str2)
    {
        rstr1 = str1;
        rstr2 = malloc(sizeof(char)*strlen(str1));
        for(i = 0 ; i < strlen(str1)-strlen(str2) ; i++)
        {
            rstr2[i] = '0';
        }
        for(i = i /*To be one spot after the first loop was at*/ ; i < strlen(str1) ; i++)
        {
            rstr2[i] = str1[i];
        }
    }
    else
    {
        rstr1 = malloc(sizeof(char)*strlen(str2));
        rstr2 = str2;
        for(i = 0 ; i < strlen(str2)-strlen(str1) ; i++)
        {
            rstr1[i] = '0';
        }
        for(i = i /*To be one spot after the first loop was at*/ ; i < strlen(str1) ; i++)
        {
            rstr1[i] = str1[i];
        }
    }
    /*After memory is allocated*/
    for(i = 0 ; i < strlen(outputstr) ; i++)
    {
        outputstr[i] = (str1[i]-'0') || (str2[i]-'0');
    }
    /*Now turn the number from binary string to decimal - pretty simple. Just go from the right to the left with powers of 2 when the first is 2 powered by zero and so on and when it's '1' in the output string - add to the output integer the 2 powered by the spot from the right*/

    return output;
}

Some explanation of Bitwise Operators and uses and all. Bitwise operators actually use the binary representation of a number and perform an action on it with another number. Example would be OR, as above. Takes all the bits, and just OR them all and get a number. What is it useful for? Actually tons of stuff. Working with graphics and colors especially, OpenGL uses them when setting a window, it defines constants and you send the Bitwise OR solution of them - and that way it knows all of the options you chose by knowing the possible option.

Here's a working C program to demonstrate a possible solution:

#include <stdio.h>
#include <string.h>

main()
{
    int i;
    unsigned int res = 0;
    unsigned int bitmask = 1;
    static unsigned char *ptr = "00000000011101";

    printf("ptr = %s\n", ptr);
    for (i=(strlen(ptr) -1); i>0; i--) {
        printf("ptr[%d] = %c (bitmask = 0x%0x)\n", i, ptr[i], bitmask);
        if (ptr[i] == '1')
            res = (res | bitmask);
        bitmask <<= 1;
    }
    printf("res = 0x%x\n", res);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top