static long calculateGrouping(Set<Integer> groupingSet, List<Integer> columns)
    {
        long grouping = (1L << columns.size()) - 1;

        for (int index = 0; index < columns.size(); index++) {
            int column = columns.get(index);

            if (groupingSet.contains(column)) {
                // Leftmost argument to grouping() (i.e. when index = 0) corresponds to
                // the most significant bit in the result. That is why we shift 1L starting
                // from the columns.size() - 1 bit index.
                grouping = grouping & ~(1L << (columns.size() - 1 - index));
            }
        }

        return grouping;
    }
@Test
    public void testGroupingOperationSomeBitsSet()
    {
        List<Integer> groupingOrdinals = ImmutableList.of(7, 2, 9, 3, 5);
        List<Set<Integer>> groupingSetOrdinals = ImmutableList.of(ImmutableSet.of(4, 2), ImmutableSet.of(9, 7, 14), ImmutableSet.of(5, 2, 7), ImmutableSet.of(3));
        List<Long> expectedResults = ImmutableList.of(23L, 11L, 6L, 29L);

        for (int groupId = 0; groupId < groupingSetOrdinals.size(); groupId++) {
            Set<Integer> groupingSet = groupingSetOrdinals.get(groupId);
            assertEquals(Long.valueOf(calculateGrouping(groupingSet, groupingOrdinals)), expectedResults.get(groupId));
        }
    }