gray-code

Gray code pattern in tournament chart?

本小妞迷上赌 提交于 2019-12-01 10:51:22
In a tournament chart from the bottom to the top where there is a winner I've been told that it is somehow connected with the gray-code. I know that the grey code is an alternative code, it's recursive and is useful to find the best solution in various games, space-filling-curves, error correction codes, harddisk positioning and is a shorthand for the piano player but how is this code is related with a tournament chart? Parsed the following from here : A tournament is really a node in a binary tree. The value in each node contains the ranking of the best ranking team contained in the

Deriving nth Gray code from the (n-1)th Gray Code

泪湿孤枕 提交于 2019-11-30 21:07:00
问题 Is there a way to derive the 4-bit nth Gray code using the (n-1)th Gray code by using bit operations on the (n-1)th Gray Code? For example the 4th Gray code is 0010. Now I want to get the 5th Gray Code, 0110, by doing bit operations on 0010. 回答1: Perhaps it's "cheating" but you can just pack a lookup table into a 64-bit constant value, like this: 0000 0 -> 1 0001 1 -> 3 0011 3 -> 2 0010 2 -> 6 0110 6 -> 7 0111 7 -> 5 0101 5 -> 4 0100 4 -> C 1100 C -> D 1101 D -> F 1111 F -> E 1110 E -> A 1010

Gray code in .NET

橙三吉。 提交于 2019-11-30 15:29:37
问题 Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented... 回答1: Use this trick. /* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. */ unsigned short binaryToGray(unsigned short num) { return (num>>1) ^ num; } A tricky Trick: for up to 2^n bits, you can convert Gray to binary by performing (2^n) - 1 binary-to Gray

Gray code in .NET

心已入冬 提交于 2019-11-30 14:51:04
Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented... Artelius Use this trick . /* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. */ unsigned short binaryToGray(unsigned short num) { return (num>>1) ^ num; } A tricky Trick: for up to 2^n bits, you can convert Gray to binary by performing (2^n) - 1 binary-to Gray conversions. All you need is the function above and a 'for' loop. /* The purpose of this function is

Gray code increment function

六眼飞鱼酱① 提交于 2019-11-30 05:54:02
问题 Without using any external counters or other state, I'm looking for an efficient function which takes an n-bit value (32 bits or thereabouts) and returns the subsequent value in a Gray code. That is: int fn(int x) { int y = gray_to_binary(x); y = y + 1; return binary_to_gray(y); } But while the binary_to_gray() function is trivial ( x ^ (x >> 1) ), the corresponding gray_to_binary() is not so trivial at all (a loop of log(n) iterations). Perhaps there is a more efficient sequence of

How do I find next bit to change in a Gray code in constant time?

☆樱花仙子☆ 提交于 2019-11-30 00:40:04
I have a small 8-bit processor which has a N-to-M decoder on some output lines - eg, for the 5 to 32 bit case, I write 00101 and bit 5 changes state. The only interface to the output is change-state, there is no read-back. The device counts rapidly (but randomly) occuring events, and should provide this count as a 'single bit changes' code to another device. The output pins are read in parallel by another device, and may be read as rapidly or as sparingly as the other device decides, so the count is necessary. I do NOT need to use the standard Binary Reflective Gray code - I can use any single

How do I find next bit to change in a Gray code in constant time?

霸气de小男生 提交于 2019-11-28 21:35:09
问题 I have a small 8-bit processor which has a N-to-M decoder on some output lines - eg, for the 5 to 32 bit case, I write 00101 and bit 5 changes state. The only interface to the output is change-state, there is no read-back. The device counts rapidly (but randomly) occuring events, and should provide this count as a 'single bit changes' code to another device. The output pins are read in parallel by another device, and may be read as rapidly or as sparingly as the other device decides, so the

Code Golf: Gray Code

元气小坏坏 提交于 2019-11-28 18:45:21
The Challenge The shortest program by character count that outputs the n-bit Gray Code . n will be an arbitrary number smaller than 1000 100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example. Note : I don't expect the program to print the gray code in a reasonable time ( n=100000 is overkill); I do expect it to start printing though. Example Input : 4 Expected Output : 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000 gnibbler Python - 53 chars n=1<<input() for x in range(n):print

The neighbors in Gray code

感情迁移 提交于 2019-11-28 11:47:11
Is there any algorithm that I can use to find the neighbors in Gray code? For small numbers is just fine to write the entire table, but if I have a number like 010 110 is a bit to much to write the entire grey code table with 6 numbers. Copied shamelessly from Wikipedia : /* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. The operator >> is shift right. The operator ^ is exclusive or. */ unsigned int binaryToGray(unsigned int num) { return (num >> 1) ^ num; } /* The purpose of this function is to convert a reflected binary Gray code number to

Code Golf: Gray Code

余生颓废 提交于 2019-11-27 11:39:03
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. The Challenge The shortest program by character count that outputs the n-bit Gray Code. n will be an arbitrary number smaller than 1000 100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example. Note : I don't expect the program to print the