Where did "shave and a haircut, two bits" come from?

Dear Mr. Alice:

Everyone is familiar with the knocking pattern that's often associated with "shave and a haircut, two bits." I was recently in Europe and noticed that it is also used there. Can you shed any light on the origin and/or meaning of this pattern?

— Bruce, downtown

Interesting question. Vague, vague answers. Have checked with lots of sources that claim to know lots of stuff about lots of subjects. The best I could do was that it might come from International Morse Code. If you translate the knocking pattern as "dash dot dot dash dot, dot dash" — that's /a (slash-a), which I'm told can mean "attention" at the beginning of a code message. If you send and receive code all day and you want to wake up your buddy who also sends and receives code, to be funny, you might tap out "attention." Code folks recognize letters, words, phrases by the rhythmic pattern they form, not letter by letter. So my money's on some form of the story that "shave and a haircut" comes from wartime telegraphers. Why "shave and a haircut"? I dunno. I'm sure we'll hear from some Alicelanders with other/better/stranger explanations. But here's a for-sure thing: Don't ever knock on a door in Mexico that way. It has a much ruder translation down there.