Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3340848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:39:18+00:00 2026-05-18T00:39:18+00:00

the formula for calculating nth gray code is : (n-1) XOR (floor((n-1)/2)) (Source: wikipedia)

  • 0

the formula for calculating nth gray code is :

(n-1) XOR (floor((n-1)/2))  
(Source: wikipedia)

I encoded it as:

int gray(int n)
{
  n--;
  return n ^ (n >> 1);
}

Can someone explain how the above formula works, or possibly its deriviation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:39:18+00:00Added an answer on May 18, 2026 at 12:39 am

    If you look at binary counting sequence, you note, that neighboring codes differ at several last bits (with no holes), so if you xor them, pattern of several trailing 1’s appear. Also, when you shift numbers right, xors also will be shifted right: (A xor B)>>N == A>>N xor B>>N.

        N                    N>>1                  gray
     0000           .        0000           .      0000           .
        | >xor = 0001             >xor = 0000           >xor = 0001
     0001          .         0000          .       0001          .
       || >xor = 0011           | >xor = 0001           >xor = 0010
     0010           .        0001           .      0011           .
        | >xor = 0001             >xor = 0000           >xor = 0001
     0011         .          0001         .        0010         .
      ||| >xor = 0111          || >xor = 0011           >xor = 0100
     0100                    0010                  0110
    

    Original Xor results and shifted results differ in single bit (i marked them by dot above). This means that if you xor them, you’ll get pattern with 1 bit set. So,

    (A xor B) xor (A>>1 xor B>>1) == (A xor A>>1) xor (B xor B>>1) == gray (A) xor gray (B)
    

    As xor gives us 1s in differing bits, it proves, what neighbouring codes differ only in single bit, and that’s main property of Gray code we want to get.

    So for completeness, whould be proven, that N can be restored from its N ^ (N>>1) value: knowing n’th bit of code we can restore n-1’th bit using xor.

    A_[bit n-1] = A_[bit n] xor gray(A)_[bit n-1]
    

    Starting from largest bit (it is xored with 0) thus we can restore whole number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the formula for calculating the distance between 2 geocodes? I have seen
Is there a tool or a formula for calculating man-hours required for a certain
I'm calculating pi using a long winded formula. I'm trying to get more familiar
I have a standard formula for calculating loan amortization schedule. Here is the formula:
I am trying to use GNU coreutil top's formula for calculating CPU usages in
I have complex formula calculating the value of a cell and it calculates the
I wanted to know how I can generate pi to the nth digit. I
I am trying to create a formula for calculating the percentage difference between two
I encounter this issue when calculating the price for a product but the formula
I am trying to use one single formula across multiple cells for calculating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.