Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
35
Task/Gray-code/PowerShell/gray-code.ps1
Normal file
35
Task/Gray-code/PowerShell/gray-code.ps1
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Gray code
|
||||
|
||||
function Encode {
|
||||
[OutputType([uint32])]
|
||||
param([uint32]$V)
|
||||
|
||||
return $V -bxor ($V -shr 1)
|
||||
}
|
||||
|
||||
function Decode {
|
||||
[OutputType([uint32])]
|
||||
param([uint32]$V)
|
||||
|
||||
[uint32]$result = 0
|
||||
while ($V -gt 0) {
|
||||
$result = $result -bxor $V
|
||||
$V = $V -shr 1
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
Write-Output "decimal binary gray decoded"
|
||||
foreach ($i in 0..31) {
|
||||
[uint32]$g = Encode($i)
|
||||
[uint32]$d = Decode($g)
|
||||
[string]$out = @()
|
||||
$out += "{0, 4} " -f $i
|
||||
$binS = ([convert]::ToString($i, 2)).PadLeft(5, "0")
|
||||
$out += "$binS "
|
||||
$binS = ([convert]::ToString($g, 2)).PadLeft(5, "0")
|
||||
$out += "$binS "
|
||||
$binS = ([convert]::ToString($d, 2)).PadLeft(5, "0")
|
||||
$out += "$binS{0, 4}" -f $d
|
||||
Write-Output $out
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue