When you print out your bool
s, the stream has a boolalpha
flag to determine whether they print out as true
/false
or as 1
/0
. If you want them to print out as true
/false
, you can do something like:
std::cout << std::boolalpha << a[i];
I wrote an answer some time ago that goes into more detail about this: https://stackoverflow.com/a/15960054/179910
For the sake of compatibility with C, C++ defined implicit conversions from int
to bool
and bool
to int
, so 0
converts to false
and any other value to true
, while false
converts to 0
and true
converts to 1
. So when you do a[2] + a[1]
, it converts each bool
to the corresponding int
, then adds those together. Not always the behavior you'd want, but something that's used in enough C code that they apparently thought it necessary to maintain anyway (and I think they were right).
Getting back to memset
, the problem with setting int
s to non-zero values is that memset
always treats the data you give it as an array of char
, regardless of the type of data that's actually there. Each char
(by definition) occupies a single byte. For example, let's assume you have 4-byte int
s on your implementation. In this case, if you use memset
to set those int
s to 1
, what you're going to get is each int
set to the value 0x01010101
(which in decimal will be 16,843,009).
In C++, std::fill
or std::fill_n
tends to work out better as a rule. It's implemented as a template so its behavior is tailored to the type being written, so if you use it to fill an array of int
with 1
, each int
will actually have the value 1
.