FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Bit-Manipulation/GrayCodes.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Bit-Manipulation
/
GrayCodes.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (38 loc) · 1.47 KB
Breadcrumbs
JavaScript
/
Bit-Manipulation
/
GrayCodes.js
Copy path
File metadata and controls
42 lines (38 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Generates a Gray code sequence for the given number of bits.
*
@param
{
number
} n - The number of bits in the Gray code sequence.
*
@returns
{
number[]
} - An array of Gray codes in binary format.
*
@description
* Gray codes are binary sequences in which two successive values differ in only one bit.
* This function generates a Gray code sequence of length 2^n for the given number of bits.
*
* The algorithm follows these steps:
*
* 1. Initialize an array `grayCodes` to store the Gray codes. Start with [0, 1] for n = 1.
* 2. Iterate from 1 to n:
* a. Calculate `highestBit` as 2^i, where `i` is the current iteration index.
* b. Iterate in reverse order through the existing Gray codes:
* - For each Gray code `code`, add `highestBit | code` to `grayCodes`.
* - This operation flips a single bit in each existing code, creating new codes.
* 3. Return the `grayCodes` array containing the Gray codes in decimal representation.
*
*resources: [GFG](https://www.geeksforgeeks.org/generate-n-bit-gray-codes/)
*
@example
* const n = 3;
* const grayCodes = generateGrayCodes(n);
* // grayCodes will be [0, 1, 3, 2, 6, 7, 5, 4] for n=3.
*/
function
generateGrayCodes
(
n
)
{
if
(
n
<=
0
)
{
return
[
0
]
}
const
grayCodes
=
[
0
,
1
]
for
(
let
i
=
1
;
i
<
n
;
i
++
)
{
const
highestBit
=
1
<<
i
for
(
let
j
=
grayCodes
.
length
-
1
;
j
>=
0
;
j
--
)
{
grayCodes
.
push
(
highestBit
|
grayCodes
[
j
]
)
}
}
return
grayCodes
}
export
{
generateGrayCodes
}
Back
|
FazBrowse Home
|
New Git URL