Fred is 31 and Barney is 27.
If the sum of their ages was 48 and Fred was 4 years older, you subtract 4 from 48 to get 44. Then you divide 44 by 2 and get 22 for each age. You add the 4 back to Fred's age and then add 5 years to both 26 and 22 to get 31 and 27.