There is a general understanding that police officers deserve implicit respect while on duty, more so than random citizens, as they are representatives of the law. Swearing at a random passerby is rude but legal, swearing at a police officer is against this need for respect and is a misdemeanor in many places around the world.