Why is gray code called reflected code? - gray-code

I understand that each gray code differs from its preceding code by one bit, but i don't exactly understand why its called reflected. I came across this website https://www.pc-control.co.uk/gray_code.htm, where it says " The gray code is sometimes referred to as reflected binary, because the first eight values compare with those of the last 8 values, but in reverse order", but the first 8 gray codes are not comparable to the last 8 gray codes in reverse order as can be seen from the gray code table on their website. To add to my confusion the gray code table differs from the gray code table on my textbook, for eg gray code for 9 = 1000 on my textbook while on the website its 9 = 1101.

Consider the sequence on the linked page:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Remove the most significant bit and you obtain a nice reflected sequence:
x000
x001
x011
x010
x110
x111
x101
x100
-------- mirror
x100
x101
x111
x110
x010
x011
x001
x000
Please note that the same kind of reflection can be found for Gray sequences of any width.

Related

how packed decimal stores values. how to write a node js logic for packed decimal of variable type PIC S9(09)V99 COMP-3

I have a variables of type PIC S9(09)V99 COMP-3 and PIC S9(07) COMP-3 in a file. I need to parse it and get the value in node js. Could you please help me understand it with simple examples. Also help me understand how data is stored for the same
Cobol natural way of working with number is Binary coded decimal a.k.a BCD.
"Classic" PIC (USAGE IS DISPLAY)
THEORY :
When you write PIC S9(4)V9(3) with a USAGE value of DISPLAY, that is to say without writing COMP-3, it is BCD.
In this way of writing number, a digit takes one octet (i.e. 8 bits) to be stored.
Each octet can be divided in two quartet, also know as nibble. A quartet is a group of 4 bits. So writing a digit takes two quartet, that is to say 2*4 bits which adds up to 8 bits = 1 octet.
When writing a digit the first quartet will hold a value wich represent the sign carried by the digit. The only digit that carry the sign is the last. The positive value is C (like credit) in hexadecimal and 1100 in binary. The negative is D (like Debit) in hexadecimal and 1101 in binary. The neutral letter is F in hex and 1111 in binary.
The second quartet will hold the binary value for your digit. For instance if my digit is 7, the quartet will be 0111.
EXAMPLE :
If I take the example I have written above PIC S9(4)V9(3), it has 4 + 3 = 7 digits. It needs 7 octets to be stored. Let's say that I want to store the number +0036.421
then the 7 octets would be 0033421
First digit 0 : 1111 0000 ; which in Hex is : F0
Second digit 0 : 1111 0000 ; which in Hex is : F0
Third digit 3 : 1111 0011 ; which in Hew is : F3
Fourth digit 6 : 1111 0110 ; which in Hex is : F6
Fifth digit 4 : 1111 0100 ; which in Hex is : F4
Sixth digit 2 : 1111 0010 ; which in Hex is : F2
Seventh digit 1 : 1100 0001 ; which in Hex is : C1. This digit carrie the sign for the whole number. The first quartet is C, because the number is positive.
COMP-3 PIC (Packed decimal)
THEORY :
As you can see you takes up a lot of space (7 octets !!!) for writing a rather small number. for comparisons, in binary with seven octets you can store 2^56 values !
So in order to keep some memory there is another format, the packed decimal, which you can use by adding COMP-3 to your declaration.
In this format instead of storing a digit in a whole octet, you just take a quartet. The trick is not to store the signs for the digits that aren't the last digit. Because as you can see above, most of the quartet just have a F value for the first quartet which convey little to no information.
The last quartet of the last octet contains the sign of the whole number. Therefore if we have n digits, we need n+1 quartets to store the number in packed decimal. So the space occupied by a number with n digits in packed decimal would be foor((n+1)/2). That is to say the lowest number of octet that contains n+1 quartet.
EXAMPLE :
My pic is 7 digits long, so I need 7+1=8 quartet = 4 octet.
First octet : 0000 0000 ; which is 00 in Hex (The first digit is 0, the second digit is 0)
Second Octet : 0011 0110 ; wich is 36 in Hex ( the third digit is 3, the fourth is 6)
Third Octet : 0100 0010 ; which is 42 in Hex ( the fifth digit is 4, the sixth is 2)
Fourth octet : 0001 1100 ; which is 1C in Hex ( the seventh digit is 1 and the sign of the whole number is +)

Insert a 0"zero" infront of number for specify number sequences

I have this data
Name | Code | Price
XXX 102 1000
YYY 4321 1150
ZZZ 202 1150
AAA 123 1000
I can now Add concatenate and Add 0 in front of Code which makes
0102
04321
0202
0123
Now here the problem lies. I dont want that 0 in front of 4321 . I want 0 only infront of 3 digit numbers not more than 3 digit.
Right click on Column, go to Format cell-->Custom and write 0000 in the type and click on Ok
Simplest and easy solution
Assuming the '102' data is located at B2, just type :
=IF(len(B2)<=3,"0"&B2,B2)
will do. Alternatively, using concatenate() function you may do it like this :
=IF(len(B2)<=3,CONCATENATE("0"&B2),B2)
Assuming you have codes in B column
if(len(b2)=3,concatenate("0",b2),b2)
If you want to write formula then this would be better,
=REPT(0,4-LEN(A1))&A1

A more natural color representation: Is it possible to convert RGBA color to a single float value?

Is it possible to represent an RGBA color to a single value that resembles the retinal stimulation? The idea is something like:
0.0 value for black (no stimulation)
1.0 for white (full stimulation)
The RGBA colors in between should be represented by values that capture the amount of stimulation they cause to the eye like:
a very light yellow should have a very high value
a very dark brown should have a low value
Any ideas on this? Is converting to grayscale the only solution?
Thanks in advance!
Assign specific bits of a single number to each part of RGBA to represent your number.
If each part is 8 bits, the first 8 bits can be assigned to R, the second 8 bits to G, the third 8 bits to B, and the final 8 bits to A.
Let's say your RGBA values are= 15,4,2,1. And each one is given 4 bits.
In binary, R is 1111, G is 0100, B is 0010, A is 0001.
In a simple concatenation, your final number would be 1111010000100001 in binary, which is 62497. To get G out of this, 62497 / 256, round it to an integer, then modulo 16. 256 is 16 to the second power because it is the 2nd position past the first from the right(R would need third power, B would need first power). 16 is 2 to the fourth power because I used 4 bits.
62497 / 256 = 244, 244 % 16 = 4.

Product attribute combinations generation in Excel

I have a table which contains 13.931 rows and 2 columns, the first column is SKU's, second is Options (Size, Colour, etc..);
SKU Option
0001 Size:S
0001 Size:M
0001 Size:L
0001 Size:XL
0001 Colour:Red
0001 Colour:Blue
0002 Size:S
0002 Size:M
0002 Size:L
0002 Colour:Navy
0002 Leg:G
and goes on like this. What I need to do is generate every combination of these options (there are 7 option types in total; some SKU's has 2 types some has 4, some non) within SKU's, so it'll be like this;
SKU Option
0001 Size:S;Colour:Red
0001 Size:M;Colour:Red
0001 Size:L;Colour:Red
0001 Size:XL;Colour:Red
0001 Size:S;Colour:Blue
0001 Size:M;Colour:Blue
0001 Size:L;Colour:Blue
0001 Size:XL;Colour:Blue
0002 Size:S;Colour:Navy;Leg:G
0002 Size:M;Colour:Navy;Leg:G
0002 Size:L;Colour:Navy;Leg:G
I can seperate Option column into 2 for option types and option values if it makes it easier.
My question is: Is it doable using macros? Because it is going to be a huge pain if I manually do these.

Hamming Distance in Block Parity

I´m sitting here and I´m not able to solve a problem related to the hamming distance.
I have a block like this:
100
000
111
When I use the vertical redundancy Check now I get:
100-1
000-0
111-1
||| |
011-0
because I add an 1 to each row and column, if I have an odd number of 1´s or I add an 0 to each row and column, if i have an odd number of 0´s.
Now I should have 6 words right?
w1: 1001
w2: 0000
w3: 1110
w4: 1010
w5: 0011
w6: 0011
Are the created (through VRC) bits words too? So are 1000 and 0110 words too?
If I compare the words and check the Hamming Distance, I get an minimal Hamming Distance of 2 (0 should not count, because if the Hamming Distance is 0, the words are the same). E.g. compare w1 and w2.
In our lecture, the professor said, that the Hamming Distance for this example is 3. How can it be 3?!
Were is my mistake? :-(
I hope someone can help me.
Have a great Sunday!

Resources