Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/Gray-code/TypeScript/gray-code.ts
Normal file
27
Task/Gray-code/TypeScript/gray-code.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Gray code
|
||||
|
||||
function encode(v: number): number {
|
||||
return v ^ (v >> 1);
|
||||
}
|
||||
|
||||
function decode(v: number): number {
|
||||
var result = 0;
|
||||
while (v > 0) {
|
||||
result ^= v;
|
||||
v >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
console.log("decimal binary gray decoded");
|
||||
for (var i = 0; i <= 31; i++) {
|
||||
var g = encode(i);
|
||||
var d = decode(g);
|
||||
process.stdout.write(
|
||||
" " + i.toString().padStart(2, " ") +
|
||||
" " + i.toString(2).padStart(5, "0") +
|
||||
" " + g.toString(2).padStart(5, "0") +
|
||||
" " + d.toString(2).padStart(5, "0") +
|
||||
" " + d.toString().padStart(2, " "));
|
||||
console.log();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue