Search code examples
chexrepresentationpolynomials

Represent a polynom on Z/2Z in hexa


I am working on polynom over Z/2Z and we represent it by an integer in hexadecimal : for example, 0xD4 is X^7+x^6+X^4+X^2 (0xD4 is 11010100 in binary) And I just want to implement a function that prints the polynom just like I did with 0xD4. So I can convert my hexadecimal number into binary and then print X^n when the nth bit equals 1... But I wonder if there is a simplest method to do this, your opinion ? (I work in C language)

Thank you in advance for your help :)


Solution

  • You don't need to convert anything. Just test

    if (poly & (1 << k)) {...}
    

    to see if the k-th power is present (i.e. the corresponding coefficient is 1 rather than 0).

    Wrap this in a loop and you're done.

    (Here, poly is the 0xD4 from your example.)