I am coming from Java to C++ and I need something similar to byte[] from Java. I can use std::vector<> for easy array like manipulation but I need answer what is practiced in C++ to use for byte manipulation uint8 or char ? ( I have lot off packing bigger integers in arrays with & 0xff and >> number so it need to be quick)
I am coming from Java to C++ and I need something similar to byte[]
Share
Assuming that
uint8is an 8 bit unsigned integer type, the main difference on a “normal” C++ implementation is thatcharis not necessarily unsigned.On “not normal” C++ implementations, there could be more significant differences —
charmight not be 8 bits. But then, what would you defineuint8to be on such an implementation anyway?Whether the sign difference matters or not depends how you’re using it, but as a rule of thumb it’s best to use unsigned types with bitwise operators. That said, they both get promoted to
intin bitwise&anyway (again on a “normal” C++ implementation) and it really doesn’t matter for&, it doesn’t cause surprises in practice. But using<<on a negative signed value results in undefined behavior, so avoid that.So, use an unsigned type. If the most convenient way for you to write that is
uint8, and you know that your code deals in octets and will only run on systems wherecharis an octet, then you may as well use it.If you want to use a standard type, use
unsigned char. Oruint8_tin order to deliberately prevent your code compiling on “not normal” implementations wherecharis not an octet.