Bit Computation Program

This is a discussion on Bit Computation Program within the C++ Programming forums, part of the General Programming Boards category; Writing my first C++ program and I'm asked to do simple computation of counting up the bits of a value ...

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

    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 . . . . .
    Last edited by cisokay; 05-13-2005 at 11:15 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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?
    Last edited by anonytmouse; 05-13-2005 at 01:49 PM.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.
    Last edited by DougDbug; 05-13-2005 at 01:25 PM.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In fact, if you use the string class in the C++ standard library, you won't even have to reverse the digits.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> 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...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial Computation Program
    By ts9818a in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2008, 07:01 PM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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