Ad
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar
  • Custom User Avatar

    I'm assuming you're asking about the left bit (aka binary) shift operators <<

    It can be confusing at first, because the same operator is overloaded by e.g. stringstream to append things to the stream.
    But originally the << and >> operators shift bits in an integer. Combined with & (and) operation, it is used here to extract 8 bit parts of the original 32 bit number.

    Take a look here https://www.educative.io/answers/what-are-bit-shift-operations . And here https://www.tutorialspoint.com/cplusplus/cpp_operators.htm .

    Given an uint32_t variable ip, you can extract four 8 bit chunks like that:

    uint8_t a = ip >> 24; // basically discard rightmost 24 bits and we're left with the leftmost 8 bits
    uint8_t b = (ip >> 16) & 0xFF; // get rid of 16 rightmost bits, and use & to mask out the 8 leftmost bits
    uint8_t c = (ip >> 8) & 0xFF; // as above, but we discard 8 rightmost bits
    uint8_t d = ip & 0xFF; // no bit shifts - the bits are in the right place, we just need to get rid of leftmost 24 bits by masking them
    

    To reverse it:

    ip = (a << 24) + (b << 16) + (c << 8) + d;
    

    Hope that helps :)

  • Custom User Avatar

    Please, help me understand how this code works. If it possible give me a tip about how to google a theory about it.

  • Custom User Avatar

    Your solution has a very subtle bug which leads to UB and might be difficult to reproduce on other compiler.

    Bug in your solution is not a kata issue.

  • Custom User Avatar

    Please, help me. In the Eclipse IDE my code solves this task correctly. But when i attempt my solution i see another result which is mistake. For example, when i input 2154959208 in my IDE i get the "128.114.17.104" which is right. Meanwhile, in CodeWars i get a mistake - "128.0.0.0". How can i solve the problem? I have written my program on c++.