Bit operations query

This is a discussion on Bit operations query within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am new to programming, and learning bit operations. I have written a code which is supposed to ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Bit operations query

    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";
    	
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    In the code below you are never allocating any memory for bitmap.

    You either need to us new to allocate memory

    Code:
        
        unsigned char *bitmap;         //bitmap array
        int i = 0;
    
        for (i=0; i<3; i++){           //for 3 chars = 3 bytes = 24 bits
            bitmap[i]=0;
        }
    Or allocate memory when creating the variable.

    Code:
        unsigned char bitmap[3];
    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thanks for the reply. I though that when I declare a pointer the memory would get dynamically allocated in the stack. Please correct me if I am wrong.
    If I make the change, I still not getting any output for
    Code:
    cout << "The final bitmap is: " << bitmap << " \n";

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Expression: (bitmap) returns a pointer to unsigned char. You are printing pointer, not the value it points to (placing array's name is placing address of its first element - here, pointer to unsigned char).

    Your code should look like this:
    Code:
    cout << (int)bitmap[0] << (int)bitmap[1] << (int)bitmap[2] << endl;
    (int) type cast is required to print it as a number, not as a character.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    nice! thanks for the reply.
    Can I ask why do I need to convert it to an int instead of displaying it as a char?
    Also, what can I do to see that I have achieved the desired bitmap?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Quote Originally Posted by rvgsd View Post
    Thanks for the reply. I though that when I declare a pointer the memory would get dynamically allocated in the stack. Please correct me if I am wrong.
    No a declaring a pointer does not allocate any memory.

    How much would it allocate?

    You, the programmer should know how much memory to allocate.

    You must either use new in C++ or malloc for C to allocate memory dynamically. Or use static allocation of the memory.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    oh..okay. thanks for explaining. I am reading on these things, but its easy to get confused, at least at this moment for me.! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit operations on different datatypes
    By jipe4153 in forum C Programming
    Replies: 5
    Last Post: 09-08-2010, 02:20 PM
  2. Bit operations?
    By yu_raider in forum C Programming
    Replies: 5
    Last Post: 03-17-2009, 11:31 AM
  3. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 06:59 AM
  4. Replies: 7
    Last Post: 12-10-2004, 07:18 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21