fixed issue #2: handle case when w != 2^k