# Bit Computation Program

• 05-13-2005
cisokay
Bit Computation Program
Writing my first C++ program and I'm asked to do simple computation of counting up the bits of a value that are (1). When the default program is ran without the flag it would output the integer values from base2 up to base16, if a flag is ran with , then it would run from base 2 to base 16 and count up the number of (1)'s in base 2 value.

./bittest 35
Code:

```Base 2: 100011 Base 3: 1022 Base 4: 203 Base 5: 120 Base 6: 55 Base 7: 50 Base 8: 33 Base 9: 38 Base 10: 35 Base 11: 32 Base 12: 2b Base 13: 29 Base 14: 27 Base 15: 25 Base 16: 23```
if ./bittest -c 35

Code:

```BitCount was 3 Base 2: 100011 Base 3: 1022 Base 4: 203 Base 5: 120 etc . . . . .```
• 05-13-2005
anonytmouse
Actually, 35 in base 8 is 43.

Code:

```Base 10: 312 312 modulo 10 = 2 312 / 10 = 31 31 modulo 10  = 1 31 / 10 = 3 3 modulo 10  = 3 3 / 10 = 0```
Does this pattern work for other bases and edge cases? If so, can you implement it in code? What other algorithm could you use?
• 05-13-2005
DougDbug
Holy cow, man! That's your first assignment????

The trick is to use strings that represent numbers... A string that looks like a number. In your case, you might not need to convert the strings to actual numbers. Have you been studying ASCII & strings this week???

I suggest you try to "build a counter" that makes a count-up string in any base. That would involve some if-statements, and you would work on one character (digit) at a time, perhaps converting that character to an integer and back.

You should start by making a counter that counts in decimal... this should make debugging easier. :)

It would probaby be considered cheating... but here are some things that would make it easier:

strtoul() // String to long, any base (2-36)
_itoa() // Integer to string, any base (2-36) non standard microsoft only
<bitset> A container class for binary.

The reason for working with strings, is that everything in the computer's memory is stored in binary... an integer is a "value", saved as binary. By default, C++ takes decimal input, and displays numbers in decimal. You can't "save a hex integer." Normally any "conversion" is done during input/output.
• 05-13-2005
cisokay
Quote:

Originally Posted by anonytmouse
Actually, 35 in base 8 is 43.

Code:

```Base 10: 312 312 modulo 10 = 2 312 / 10 = 31 31 modulo 10  = 1 31 / 10 = 3 3 modulo 10  = 3 3 / 10 = 0```
Does this pattern work for other bases and edge cases? If so, can you implement it in code? What other algorithm could you use?

This can be implemented. I guess I would have a recursive call that would check if the value != 0, if so, it would mod the value by the base? I just read that once bases are above 10, you increment with the chars starting from 'A' and above. I'm not exactly sure how to start the method, and a method would probably be best for a function like this if I have to print out bases from 2-20. In your example it show that the value of xx in base 10 is 312. The answer is outputed in reverse. I'm not sure how to actually get that to print back out properly. Is is prefered to print the value back out as a string or int value? Thank you for your help and time on my first cpp program.
• 05-13-2005
swoopy
>Is is prefered to print the value back out as a string or int value?
I think a string might be a little easier. This is because bases greater than 10 will use characters for some of their digits anyway. Also, you can print the string in one statement.
• 05-13-2005
swoopy
In fact, if you use the string class in the C++ standard library, you won't even have to reverse the digits.
• 05-13-2005
anonytmouse
>> I guess I would have a recursive call that would check if the value != 0, if so, it would mod the value by the base? <<

You're on the right track, although you could avoid recursion if you wanted.

>> I just read that once bases are above 10, you increment with the chars starting from 'A' and above <<

Once you have a digit, as a number, you need to convert it to a character. You could do this with a bunch of if statements, a switch or something far more efficient which I'll leave you to figure out.

>> I'm not sure how to actually get that to print back out properly. <<

The std::string class has a method insert that inserts a character at a specified position. By using position 0, you can insert characters at the front.
Code:

`result.insert(0, '1');`
>> I'm not exactly sure how to start the method <<

I'd start like this:
Code:

```std::string IntegerToString(int num, int base) { // Your code here... }```