📄 graycodecounter.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * Gray.java * * Created on May 23, 2002, 2:51 PM */package Examples.QuasiMonteCarlo;/** <p>Prints the grey codes gray(n), n=1,2,...,299 computed both directly and * recursivley by changing (XORing with 1) the relevant bit. * Done to see how irregularly the grey counter runs through the integers.</p> * * <p>Recall that we use gray code counting to move through the integers in * our implementations of low discrepancy sequences based on base b=2.</p> * * @author Michael J. Meyer */public class GrayCodeCounter { /** Print binary string representation of a positive integer n. * Most significant digit leftmost as usual. */ public static void printbin(int n) { if(n>0) { printbin(n/2); System.out.print(n%2); } } // position j of the rightmost zero bit in n, counting starts from zero public static int thebit(int n) { int j=0; while(n%2==1){ n=n>>1; j++; } return j; } public static int twopower(int j) { int m=1; for(int i=0;i<j;i++)m*=2; return m; } /** The Gray code of n. */ public static int gray(int n){ return n^(n/2); } public static void main(String[] args) { int n=1, m=1; while(n<257){ // grey code directly and recursive by changing the bit System.out.print(gray(n)+", "+m+"\n"); int j=thebit(n); m=m^(1<<j); n++; } } // end main }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -