It took 12 years and 262 episodes, but I finally have something good to say about “Two and a Half Men”: Now, that’s how you do a finale.

CBS’s long-running sitcom ended its life Thursday in an hour-long orgy of sharp self-referential jokes, surprising guest stars and the tittering admission that series creator Chuck Lorreand gang had been making gobs of money selling dumb jokes to the viewers.

The show that reveled in being the lowest of the low-brow revealed in its last gasp that it was guided all along by a high level of intelligence. Of course, anyone who rises to the level of a writer or producer of network television is extremely smart and capable, but their work doesn’t always reflect that. Sometimes they’re just making a product for mass consumption.

“Two and a Half Men” was clearly that, a carefully calculated creation of reprehensibility, purveying as many crude jokes as it could slip past the CBS censors. In its early days, simply the shock value of a show built around characters with no redeeming qualities might have seemed refreshing, but nothing is shocking for 12 years.

Still, we must admit that Lorre and company were very good at what they did, and folks who wanted to roll around in the mud for 30 minutes a week found a pit full of slick fun – at least while Charlie Sheen was around.

Sheen famously left the show after eight seasons amid a very public squabble with Lorre. Lorre killed off Sheen’s character, Charlie Harper, and Sheen went off to play essentially the same character in the FX comedy “Anger Management.”

In the finale, we learn that Charlie hadn’t really been killed but was being kept captive in Sherman Oaks by his last wife, Rose (Melanie Lynskey). Charlie escapes and vows to wreak revenge on his brother, Alan (Jon Cryer) and his replacement Walden (Ashton Kutcher).

The episode was chock full of jokes about the show, about its urgent need to wrap things up and, especially, about the still-burning feud with Sheen.

Guest stars abounded: Arnold Schwarzenegger, John Stamos, Courtney Thorne-Smith, Judy Greer, Christian Slater, and even the original half man, Angus T. Jones, who put aside his anger with the show long enough to make a clever cameo.

The one guest Lorre couldn’t land, though, was Sheen, who had been in negotiations for a return but ultimately balked.

In the final scene, a stand-in for Sheen got a piano dropped on his head, then Lorre, breaking down the fourth wall, appeared in the director’s chair and declared, “Winning,” to the camera – before getting a piano dropped on his own head.

That wasn’t enough of a last word for Lorre, who finished up the series with a title card explaining why Sheen wasn’t there. It was a step too far – but perhaps that was fitting for a series that never recognized a boundary.