Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
10
Task/Gray-code/Groovy/gray-code-1.groovy
Normal file
10
Task/Gray-code/Groovy/gray-code-1.groovy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def grayEncode = { i ->
|
||||
i ^ (i >>> 1)
|
||||
}
|
||||
|
||||
def grayDecode;
|
||||
grayDecode = { int code ->
|
||||
if(code <= 0) return 0
|
||||
def h = grayDecode(code >>> 1)
|
||||
return (h << 1) + ((code ^ h) & 1)
|
||||
}
|
||||
21
Task/Gray-code/Groovy/gray-code-2.groovy
Normal file
21
Task/Gray-code/Groovy/gray-code-2.groovy
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def binary = { i, minBits = 1 ->
|
||||
def remainder = i
|
||||
def bin = []
|
||||
while (remainder > 0 || bin.size() <= minBits) {
|
||||
bin << (remainder & 1)
|
||||
remainder >>>= 1
|
||||
}
|
||||
bin
|
||||
}
|
||||
|
||||
println "number binary gray code decode"
|
||||
println "====== ====== ========= ======"
|
||||
(0..31).each {
|
||||
def code = grayEncode(it)
|
||||
def decode = grayDecode(code)
|
||||
def iB = binary(it, 5)
|
||||
def cB = binary(code, 5)
|
||||
printf(" %2d %1d%1d%1d%1d%1d %1d%1d%1d%1d%1d %2d",
|
||||
it, iB[4],iB[3],iB[2],iB[1],iB[0], cB[4],cB[3],cB[2],cB[1],cB[0], decode)
|
||||
println()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue