Java homework- what's wrong with my code? : Here's the question:

A string contains several X's followed by several O's. Devise a divide-and-conquer algorithm that finds the number of X's in the string in log2n steps, where n is the length of the string.

And here's my code:
   
    public static int count(String str){
		int min = 0;
		int max = str.length() - 1;
		while(min < max){
			if(str.charAt(min) == 'X')
				min += (max - min) / 2;	
			else
				max = min + (max - min) / 2;
		}
		return min;
	}

It seems to work most of the time but there are a few cases where it gives unexpected output. For example, when I put give it the string "XXXXOOOO" it should return 4, but it returns 5 instead. Why?
