For each even positive integer \(x,\) let \(g(x)\) denote the greatest power of 2 that divides \(x.\) For example, \(g(20)=4\) and \(g(16)=16.\) For each positive integer \(n,\) let \(S_n=\sum_{k=1}^{2^{n-1}}g(2k).\) Find the greatest integer \(n\) less than 1000 such that \(S_n\) is a perfect square.

(第二十四届AIME1 2006 第13题)