It is likely because beans and lentils are not broken down into simple sugars in the stomach. This means that they ferment more slowly, so gas production can happen more easily than with other foods such as white bread or pasta.