tags:

views:

376

answers:

3

I'm trying to write a small program that will generate a parity bit for Hex and Binary values pulled from an ASCII character array. I have a total of 3 character arrays. For example:

const char *caASCII[] = {"A", "B", "C", ...};

const char *caASCIIBinary[] = {"01000001", "01000010", "01000011", ...};

const char *caASCIIHex[] = {"41", "42", "43", ...};

So, I type "A" and it finds the corresponding values in the Binary and Hex arrays and then displays them. I have a linear search function that does the search and it works fine.

I want to know if it's possible to count, for example, the number of times that "1" occurs in one of the binary values and then judging from that (if the number of 1s is even or odd) add a "0" or a "1" at the end of the binary value. The hex values I guess I would have to divide by 2 to see if it's even or odd.

I'm starting to think that I would have to change the arrays to a different type, probably integer. Any suggestions as to how I can approach this?

A: 

Are you just looking for a function to count characters? You can easily write one yourself:

size_t strcnt(const char *s, char c) {
  size_t count = 0;
  while (*s)
    if (*s++ == c) 
      count++;
  return count;
}

You would use it like this:

#include <iostream>
// ...

size_t count = strcnt(caASCIIBinary[2], '1')
std::cout << count << " bits set in " << caASCIIBinary[2] << std::endl;
sth
I'm not really sure I understand how to use that. I assume I would want to point to one of the arrays and the second variable is the character to search for? Can you give an example as to how to use this?
bad_sofa
+1  A: 

The idea of three array is a huge nonsense: those are information easily calculated by few operations. For example, to know the numbers of "1" in the binary rep:

  int bits_on(char yourchar) {
    int count = 0;
    while (yourchar > 0) {
      count += yourchar % 2;
      yourchar >>= 1;
    }
    return count;
  }

to "add a 1 if it is even"

int newInt = yourChar << 1;
newInt += bits_on(yourchar) % 2 == 0 ? 1 : 0;
akappa
A: 

For the direction answer to your question, this counts the number of 1's in a binary string:

#include <algorithms>
#include <string>

const std::string caASCIIBinary[] = {"01000001", "01000010", "01000011", ...};

int num_ones = std::count(caASCIIBinary[5].begin(), caASCIIBinary[5].end(), '1');

If you were absolutely fixed on using char *'s instead of strings (don't do this):

int num_ones = std::count(caASCIIBinary[5], caASCIIBinary[5]+strlen(caASCIIBinary[5]), '1');

However, I'm not sure this is what you actually want to do. It sounds like, given a char, you want to output a string that has a parity bit attached:

#include <bitset>
#include <limits.h>
#include <algorithms>
using namespace std;

string parity_string(char ch){
   string str(bitset<CHAR_BIT>(ch).to_string<char,char_traits<char>,allocator<char> >());
   if(count(str.begin(), str.end(), '1') % 2) { str.append("1"); }
   else { str.append("0"); }
   return str;
}

Might be an easier way to do it by constructing the bitset with the parity bit already attached, but I'd have to think about it.

Edit: Bam:

string parity_string(char ch){
   bitset<CHAR_BIT+1> bs(ch);
   bs[CHAR_BIT] = bs.count() % 2;
   return bs.to_string<char,char_traits<char>,allocator<char> >();
}
Todd Gardner