Section 40. Whoever, having been convicted, upon indictment, of larceny or of being accessory to larceny before the fact, afterward commits a larceny or is accessory thereto before the fact, and is convicted thereof upon indictment, and whoever is convicted at the same sitting of the court, as principal or accessory before the fact, of three distinct larcenies, shall be adjudged a common and notorious thief, and shall be punished by imprisonment in the state prison for not more than twenty years or in jail for not more than two and one half years.