I am trying to initialize a bitmap to all bits on. My idea was to initialize an array of the buffer size and then use memcpy
; however:
#include <stdio.h>
#define SIZE 5
int main () {
int i[SIZE] = { ~0 };
for (int j = 0 ; j < SIZE ; j++) {
printf("%d\n", i[j]);
}
return 0;
}
results in:
-1
0
0
0
0
my expectation was that I would see all -1
. Why does this not work, and how can I accomplish my goal?
This:
int i[SIZE] = { ~0 };
Explicitly initializes only the first element of the array to ~0
. The rest are implicitly set to 0.
If you want all bits to be set, you need to use memset
:
memset(i, ~0, sizeof i);