Bit Computation

This is a discussion on Bit Computation within the C++ Programming forums, part of the General Programming Boards category; I'm given a string that is of binary 1's and 0's and I'm trying to make a function that is ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Bit Computation

    I'm given a string that is of binary 1's and 0's and I'm trying to make a function that is able to set the bits to the location that is specified.

    If 35 represented in 100011

    If I wanted to set the 3rd bit to a 1,

    It would look like 100111

    Similiarly to 0's as well.

    I've found that my function will set it, but then it truncates the values or does something funny.

    Code:
    int setOne(string value, int position)
    {
            int length = value.length();
            int temp;
            string t = value;
    
            value[length-(position+1)] = '1';
    
            temp = atoi(t.c_str());
            cout << temp << "\n";
            return temp;
    }

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    if you do
    Code:
    string s = "0";
    setOne(s,3);//should result int 1000
    note that string is only 1 char long, therefore you need first to lengthen it
    then you're passing value as being a copy of the original string. You should use a pointer or a reference to afect the original string
    And, you return temp which is a integer conversion of the original string not the changed one.
    To finish don't store the number inside a int, because atoi will asume base 10. A 32 bit int will be 32 bits long but only 10 decimal digit long. Therefore if you have a 32bit number representation inside the string atoi will return overflowed number.
    Code:
    int setOne(string& value, int position)
    {
            while(position >= value.length())
                    value = "0"+value;
            int length = value.length();
            int temp;
            //string t = value;
    
            value[length-(position+1)] = '1';
    
            temp = atoi(value.c_str());//not recomended
            cout << temp << "\n";
            return temp;
    }
    Should work...
    I would do it like
    Code:
    void set_one(string& s, unsigned int pos){
    	while(pos >= s.length())
    		s = "0"+s;
    	s[s.length()-1-pos]='1';
    }
    Last edited by xErath; 05-15-2005 at 10:23 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Thanks that works great, your refraction shortens the lines of code. Now I got the conversion to work and able to work with it in binary. I have to convert it back. I made a function that used atoi(binaryvalue); but for some reason it gave me back the same value. I think there might of been some typos along the way. Is there a function that would convert 100111 back to its decimal form, 39?
    Last edited by cisokay; 05-15-2005 at 11:02 PM.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You could write your own but there isnt a standard function to do it. Work out the length of your bitstring then work back from the end adding up as you go. you may find pow() useful.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    don't use pow.. use bit shifting

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    pow is easier for beginners especially as data type is a string.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    24
    bit shifting wouldn't be very useful if he plans to use character arrays. It would be more logical to use binary data in the first place of course.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Is there a function that would convert 100111 back to its decimal form, 39?
    Yes!!! strtoul() (String to unsigned long)

    (It's a C function, so it uses a C-Style string... a character array.)

    Actually, it doesn't change it to decimal... it changes to a regular 'ol long integer, which will dsplay in decimal by default.

    Code:
        int Base, x;
        char InputString[40];
        char *pEnd = NULL;          // Required for Strtoul()
    
        cout << "Base? (2-36) " ;
        cin >> Base;
        cin >> InputString;
    
         x = (int)strtol(InputString, &pEnd, Base);     // String to long
    Last edited by DougDbug; 05-16-2005 at 04:37 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    strtoul() is not an ANSI function, I don't think, so it might not work on cisokay's compiler.

  10. #10
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    SOLUTION
    if you want to set a bit use a function like this:
    Code:
    SetOne(int* value, int position, BOOL On)
    {
        if(On)
            *value += 1 << position;
        else
            *value -= 1 << position;
    }
    THE WAY IT WORKS
    If you have a number, say 35, it would look like this in binary:
    Code:
    100011
    If you want to turn on the third bit it, you want it to look like this:
    Code:
    100111
    You are actually adding the binary number
    Code:
    100
    to your number. 100 in binary is equal to 8 in base ten. If I am correct, 8 is 2 to the third power. You wanted to set the third bit right? Also in case you are rusty, <<'s multiply the number on the right by 2 to the power on the left. If you wanted to multiply 6 by 2 to the 4th power you would use:
    Code:
    6 << 4;
    Hope this clears things up.

    Oh, I almost forgot. Set On to TRUE to turn the bit on and FALSE to turn the bit off.
    Don't quote me on that... ...seriously

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >strtoul() is not an ANSI function, I don't think, so it might not work on cisokay's compiler.
    Are you positive? Because I was pretty sure it was a standard C library function.

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Stoned_Coder
    pow is easier for beginners especially as data type is a string.
    I'm not getting where the pow function would be used...
    Code:
    int to_dec(char *n){
        int res=0;
        .........
        return res;
    }
    I'm waiting cisokay to post something, so I can post the my function.

  13. #13
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    you could just use the bitset class. If you need a string just use the to_string(); member function if you need a unsigned long int then use to_ulong(); member function. if you need to convert from string then loop through the string doing comparisions and then set the apporpriate bit using the set(pos,true) member function.
    Last edited by manofsteel972; 05-16-2005 at 07:19 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I'm not getting where the pow function would be used
    never noticed how the bits go up in powers of 2??

    128 64 32 16 8 4 2 1

    Kinda suggests a use of pow() to me.
    And it was only a suggestion, theres many ways to do it. And I forgot strtoul(). Its not a function that I have ever used.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    The function "pow()" is a good function for getting powers of numbers. However, if you are going by 2's as suggested, you can shift.

    a << b

    This is just like multiplying:

    a x b

    except rather than 'a' times 'b', it does 'a' times 2 to the power of 'b'. If you set 'a' to 1, then you just get 2 to the power of 'b'.

    1 << 3

    is the same as 2 to the power of 3 which is eight. If you are trying to keep speed at a maximum then shifting is the way to go. Just the function call to pow() takes more time than shifting. However, it's just a suggestion. Pow() will work fine if you don't want to or don't feel up to shifting.
    Don't quote me on that... ...seriously

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  2. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21