0

I am working on a C++ program which implements some encryption algorithms (AES, Blowfish) and I need to obtain some metrics for evaluation. Specifically I need to measure the data of an encryption/decryption operation in kilobytes per second. This operation would use the same plain text data to measure different algorithms. Part of this is simply making use of a timer to obtain the seconds, and the second part is to count the number of bits. From here I can calculate the kilobytes per second.

The issue I'm trying to solve is how to measure the number of bits passing through the algorithms. The algorithms take input in the format of an unsigned character array of fixed size (8 or 16 characters depending on the algorithm). My initial solution was to count the number of characters passing though the algorithm and take this as the number of bytes. The bytes multiplied by 8 would then presumably be the bit count, however I've read that a character is not necessarily always an 8 bit byte. A discrepancy in the bit count could significantly skew results.

How exactly would I obtain the number of bits in an unsigned char array? (Or the number of bits of a single char element in that array).

x2kpb
  • 136
  • 4
  • If you're working on a C++ program, then why did you tag the question with `c`? While C and C++ might be related and share some syntactic similarities they are still two very different languages with different rules. – Some programmer dude Jul 24 '16 at 13:22
  • The implementations of the encryption algorithms in my program are public domain, they are written in C. My program (which is similar to a wrapper) is in C++. I've removed the C tag as you're correct about the semantic differences. – x2kpb Jul 24 '16 at 13:23
  • 2
    Unless you are working on an exotic (and most likely obsolete) architecture, the number of bits in a character is precisely 8. There is already a mountain of code out there that makes this assumption (and it's in POSIX as well); adding a few more lines doing the same thing is not going to hurt anymore now. See http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char – H. Guijt Jul 24 '16 at 13:34
  • The program will run on windows, linux and potentially MAC, nothing else. My concern with regards to the char being represented by 8 bits was raised by the conclusion ("the number of bits in a char must be at least 8") in the answer to this question: http://stackoverflow.com/questions/881894/is-char-guaranteed-to-be-exactly-8-bit-long – x2kpb Jul 24 '16 at 13:44
  • A much more relevant worry is whether `char` is signed or unsigned. `signed char`, `unsigned char` and `char` are *three distinct types* and the signedness of the `char` type is implementation defined and *does* vary across modern/relevant platforms. – Jesper Juhl Jul 24 '16 at 13:50
  • 1
    You're right about that being an issue (it simply hasn't caused a problem for me yet), I'll look at modifying the program to use uint8_t as you suggested in your answer. – x2kpb Jul 24 '16 at 13:58

1 Answers1

0

char is highly unlikely to be anything but 8bits, but if you need to be 100℅ certain, then use int8_t/uint8_t instead. uint8_t is, in any case, a much more natural type than char for binary data - it avoids the problem of whether char is signed or unsigned on any given platform.

For number of bits in char, see this existing question: Get number of bits in char

Community
  • 1
  • 1
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • sizeof(char) is the number of chars in a char, in other words it is always 1. – H. Guijt Jul 24 '16 at 13:35
  • Ahh crap. Just double checked and you are right. I was sure it was bytes. Removed that bit. Thanks. – Jesper Juhl Jul 24 '16 at 13:37
  • This solved my problem. The following code from your suggestion: sizeof(char) * CHAR_BIT, works with char (or uint8_t as you mentioned) – x2kpb Jul 24 '16 at 13:51