The legendary Warren Buffett once wrote: "A simple rule dictates my buying: Be fearful when others are greedy, and be greedy when others are fearful." He shared that timely advice amid the Great Recession in 2008, which makes it especially relevant right now.

Inflation has hammered the U.S. economy this year, reaching levels not seen in four decades, and the Federal Reserve is raising interest rates so aggressively that many economists and finance experts see a recession on the horizon. Those worrisome events have sent the stock market tumbling. The S&P 500 and the Nasdaq Composite have now declined for three consecutive quarters -- that hasn't happened since 2009 -- and both indexes have dropped into a bear market.

Here's what investors should know.

Advice from Warren Buffett, Shelby Davis, and Peter Lynch

Shelby Davis is one of the most successful investors in history. Davis was in his late thirties when he started investing in 1947, and he managed to turn an initial sum of $50,000 into $900 million by the time he died in 1994. Davis battled eight bear markets throughout his career, and he saw his portfolio decline by 60% at one point. But Davis treated those downturns as buying opportunities. In fact, he once said, "You make most of your money in a bear market; you just don't realize it at the time."

Peter Lynch is another storied investor. He managed the Magellan Fund at Fidelity between 1977 and 1990. That 13-year period was characterized by two bear markets and six market corrections, but Lynch doubled the performance of the S&P 500, achieving an annualized return of 29.2%. He once offered this advice: "A correction is a wonderful opportunity to buy your favorite companies at a bargain price."

Warren Buffett, Shelby Davis, and Peter Lynch all have one thing in common. They know downturns are an excellent time to buy stocks. But not every beaten-down stock is worth buying.

In his 1995 letter to shareholders, Buffett explained his thoughts: "In business, I look for economic castles protected by unbreachable moats." All investors should adhere to that advice, and Microsoft (MSFT -0.14%) is a great example.

Microsoft is a resilient business protected by a strong moat

Microsoft's broad portfolio of software products and cloud services has helped it cultivate immense brand authority. In fact, Brand Finance named Microsoft the fourth-most-valuable brand in the world in 2022, and it ranked CEO Satya Nadella as the top chief executive.

Microsoft recently reported earnings for the first fiscal quarter of 2023 (ended Sept. 30, 2022). Revenue climbed 11% to $50 billion, but guidance fell short of expectations due to macroeconomic headwinds. That sent the stock down. Microsoft has now seen its share price fall more than 30% this year, marking its greatest loss of value at any point in the past decade.

Investors need to look at the big picture. High inflation and unfavorable foreign exchange rates are temporary problems, and Microsoft is a resilient business protected by an arsenal of mission-critical applications. Office365 is the gold standard in productivity, but other software products have also achieved a strong market presence, such as Dynamics 365 in enterprise resource planning, Teams in communications, and Power BI in business intelligence.

Better yet, Microsoft has been recognized as a leader in several categories of cybersecurity software, and that portion of its business is growing like wildfire. Security revenue soared 40% in fiscal 2022 (ended June 30, 2022), and Microsoft's security customer count climbed 33% to 860,000 in the most recent quarter. Looking ahead, the cybersecurity market is expected to grow at 12% annually through the end of the decade, according to Grand View Research.

Also noteworthy, Microsoft Azure is the second-largest cloud services provider, and the company has been gaining market share. According to Canalys, Azure accounted for 24% of cloud infrastructure spend in the June quarter, up from 18% three years earlier. That bodes well for Microsoft. Cloud computing spend is expected to grow at 16% annually through 2030, according to Grand View Research.

Put simply, investors have good reason to think Microsoft can maintain double-digit revenue growth for years to come, and with shares trading at 25.4 times earnings -- a bargain compared to the three-year average of 32.2 times earnings -- this growth stock is worth buying in a bear market.