Hi all,

I am new to programming, and learning bit operations. I have written a code which is supposed to set selected bits in a character array to 1 from 0.

When I run this code, I get segmentation fault as output.

I tried to find similar examples on internet, but all explain bit operations but none explain with any sample codes.

Any help would be greatly appreciated.

Code:main(){ unsigned char *bitmap; //bitmap array int i = 0; for (i=0; i<3; i++){ //for 3 chars = 3 bytes = 24 bits bitmap[i]=0; } int base_bit = 3, //trying to convert 00000000 00000000 00000000 bits_from_base = 15, // to 00111111 11111111 11000000 bit_index=0; for (i=base_bit/8; i<(base_bit+bits_from_base)/8; i++){ if( i==base_bit/8 ){ //First byte:change the bit_index and all last bytes after it to 1 bit_index = i%8; while(bit_index<8){ bitmap[i] |= (0x10>>bit_index); bit_index++; } } else if( i==(base_bit+bits_from_base)/8 ){//Last byte:change the first bytes till bit index to 1 bit_index = i%8; while(bit_index>=0){ bitmap[i] |= (0x10>>bit_index); bit_index--; } } else{ //all the bytes between first and last bitmap[i] |= 0x11; } } cout << "The final bitmap is: " << bitmap << " \n"; }