2018-09-30 23:01:58 +01:00
---
id: 5a23c84252665b21eecc7e80
2020-11-27 19:02:05 +01:00
title: Gray code
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302276
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
[Gray code ](<https://en.wikipedia.org/wiki/Gray code> ) is a form of binary encoding where transitions between consecutive numbers differ by only one bit.
2018-10-08 01:01:53 +01:00
This is a useful encoding for reducing hardware data hazards with values that change rapidly and/or connect to slower hardware as inputs.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
It is also useful for generating inputs for [Karnaugh maps ](<https://en.wikipedia.org/wiki/Karnaugh map> ) in order from left to right or top to bottom.
# --instructions--
2019-03-05 14:39:16 +09:00
Create a function to encode a number to and decode a number from Gray code. The function should will have 2 parameters.
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
The first would be a boolean. The function should encode for true and decode for false. The second parameter would be the number to be encoded/decoded.
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary).
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
There are many possible Gray codes. The following encodes what is called "binary reflected Gray code."
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
Encoding (MSB is bit 0, b is binary, g is Gray code):
2020-11-27 19:02:05 +01:00
< pre > if b[i-1] = 1
2019-03-05 14:39:16 +09:00
g[i] = not b[i]
else
g[i] = b[i]
< / pre >
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
Or:
2020-11-27 19:02:05 +01:00
< pre > g = b xor (b logically right shifted 1 time)
2019-03-05 14:39:16 +09:00
< / pre >
2020-11-27 19:02:05 +01:00
2019-03-05 14:39:16 +09:00
Decoding (MSB is bit 0, b is binary, g is Gray code):
2020-11-27 19:02:05 +01:00
< pre > b[0] = g[0]< br >
2019-03-05 14:39:16 +09:00
for other bits:
b[i] = g[i] xor b[i-1]
< / pre >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
`gray` should be a function.
```js
assert(typeof gray == 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`gray(true,177)` should return a number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof gray(true, 177) == 'number');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`gray(true,177)` should return `233` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.equal(gray(true, 177), 233);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`gray(true,425)` should return `381` .
```js
assert.equal(gray(true, 425), 381);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`gray(true,870)` should return `725` .
```js
assert.equal(gray(true, 870), 725);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`gray(false,233)` should return `177` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(gray(false, 233), 177);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`gray(false,381)` should return `425` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(gray(false, 381), 425);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`gray(false,725)` should return `870` .
```js
assert.equal(gray(false, 725), 870);
```
# --seed--
## --seed-contents--
```js
function gray(enc, number) {
}
```
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function gray(enc, number){
if(enc){
return number ^ (number >> 1);
}else{
let n = number;
while (number >>= 1) {
n ^= number;
}
return n;
}
}
```