Image Blurring (Bitwise) problem

Hi everybody ;D
I’m not really new to the forum but that is the first time I’m posting something.
I have been working on a little project inspired by “Zen’a photo garden”,and I need to blur the image in that project. So I decided to learn how to do blurring. I’ve searched for the algorithm and I think I understand it. However I have a problem with the color channels. Here is my code:

public void blur(BufferedImage image) {
		//Note: the BufferedImage is of the type "INT_RGB"
		
		// set pixels
		int[] pixelData = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
		//Second array so pixels don't get overwritten while still needing them
		int[] pixels = pixelData.clone();
		
		//main loop over pixels
		for (int n = 0; n < pixelData.length; n++) {
			//Separate channels
			int r = 0, g = 0, b = 0;

			//loops to get neighbor pixels in the x direction
			for (int i = -1; i < 1; i++) {
				//discarding exceptions for the first and last pixels
				try {
					r += (pixelData[n + i] >> 16) & 0xff0000;
					g += (pixelData[n + i] >> 8) & 0x00ff00;
					b += (pixelData[n + i] >> 0) & 0x0000ff;
				} catch (ArrayIndexOutOfBoundsException e) {
					
				}
			}
			
			//averaging the colors of the pixels
			r /= (3);
			g /= (3);
			b /= (3);
			
			//final pixel
			pixels[n] = r | g | b;
		}
		
		//writing pixel data to the image
		image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
	}

This is the method that I call to blur the image. As you can see (and I can’t :wink: ) there is a problem with it. It outputs different colors even when I’m using black and white. I believe the mistake is in the bitwise operations. I’ve tried various methids to get it to work but with no avail :frowning: .

If someone could explain to me what is wrong with my code and how can I solve it that would be great ;D

PS: I know that this method only takes neighboring pixels in the x direction, and I’m planning to make it take circular shaped neighboring. But that is just for demonstration.