Tax law favors someone who bought real estate in San Francisco over someone who bought skyrocketing stock in the region’s tech companies.

Of the many winners and losers in the overhaul of the tax code, one change makes real estate investors the biggest beneficiaries, while art collectors seem to have drawn the short straw.

Real estate investors were given a gift after Congress voted to maintain what are known as 1031 exchanges, a section in the tax code that allows for property to be sold tax-free as long as the proceeds are used to buy more property. The loophole had been open to others as well, including art collectors, classic car aficionados and franchisees, but not any longer.

Investors whose real estate holdings are comparatively modest — and their heirs — were given an added bonus: The estate tax exemption for couples doubled to $22.4 million, allowing those investors to conceivably pay no tax on their properties, ever. They can use exchanges to buy ever more valuable property, and when they die, all of the capital gains are erased, so their heirs inherit the real estate at whatever it’s worth at the time.

The exchange loophole would appear to help wealthy real estate developers like President Trump, whose fortune is made up of commercial properties, and his son-in-law, Jared Kushner, who also hails from a real estate family.

“People keep rolling it over,” said Christopher Pegg, senior director of wealth planning for California and Nevada at Wells Fargo Private Bank. Investors can continue to take advantage of the exchanges until their death, at which point the capital gains tax is eliminated, he said. “You get that big basis step-up in the sky, and the tax is entirely avoided.”

And the cost for investors to save millions of dollars in taxes? A few thousand dollars in fees to the firm that manages the exchange proceeds.

This preferential treatment is great if you’re a real estate investor. But it’s baffling for anyone else and may be downright infuriating to homeowners in high-tax states who could end up paying more in taxes because of limits placed on their ability to deduct property taxes on their federal income tax.

Take, for example, someone who bought real estate in San Francisco 20 years ago versus someone who bought an equivalent amount of stock in Apple, based nearby in Cupertino. Both assets have appreciated wildly and made a tremendous amount of money for their investors.

But here’s where the investors’ fortunes diverge. If the owner of Apple stock sold it all and put the proceeds into other technology companies, the stock sale would be subject to a capital-gains tax of 20 percent.

If the investor who bought real estate in San Francisco sold the property, the money could be used to buy another property in San Francisco — or anywhere in the country — without the seller’s paying a tax on the appreciated gains in the original property.

“From the I.R.S.’s perspective, the taxpayer gives up property and what they receive is other like-kind property,” said Matthew K. Scheriff, a certified public accountant and executive vice president of Legal 1031 Exchange Services….