1. ## 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. 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';
}```

3. 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?

4. 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.

5. don't use pow.. use bit shifting

6. pow is easier for beginners especially as data type is a string.

7. 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. 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```

9. strtoul() is not an ANSI function, I don't think, so it might not work on cisokay's compiler.

10. 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.

11. >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. 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. 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.

14. 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.

15. 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.