Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Complete Gray code
Browse files Browse the repository at this point in the history
  • Loading branch information
mendax1234 committed Aug 16, 2023
1 parent dc9c096 commit f7e4f28
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ On the first class of SC1005, l was confused about Gray code and l found that ma
In short, the advantage of Gray code is that juts one bit changes for each step.

## How to convert Binary to Gray Code?
> Thanks this [link](https://zhuanlan.zhihu.com/p/451408535) for helping me to gain a deeper understanding of Gray code.
### Things you need to know before starting
1. The [**XOR operation**](https://www.baeldung.com/java-xor-operator)


###
### Binary to Gray code Conversion
1. The Most Significant Bit (MSB) of the gray code is always equal to the MSB of the given binary code.
2. Other bits of the output gray code can be obtained by XORing binary code bit at that index and previous index.

<p align="center"> <img src="/blog/assets/binary-to-gray-code/Gray-Code1.png" title = "Binary code to gray code conversion"> </p>

### Gray code to Binary Conversion
1. The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given gray code.
2. Other bits of the output binary code can be obtained by checking the gray code bit at that index. If the current gray code bit is 0, then copy the previous binary code bit, else copy the invert of the previous binary code bit.

<p align="center"> <img src="/blog/assets/binary-to-gray-code/Gray-Code2.png"> </p>

## Acknowledgement
- [**The definition of Gray Code on Wikipedia**](https://en.wikipedia.org/wiki/Gray_code)
- [**The XOR operation**](https://www.baeldung.com/java-xor-operator)
- [**The conversion between Binary and Gray Code**](https://www.geeksforgeeks.org/gray-to-binary-and-binary-to-gray-conversion/)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ nav:
- 2023:
- blog/posts/hello-world.md
# - blog/posts/ntu-preschool.md
- blog/posts/binary_to_gray_code.md
- blog/posts/binary-to-gray-code.md

markdown_extensions:
- footnotes
Expand Down

0 comments on commit f7e4f28

Please sign in to comment.