0

I'm trying to understand what this function does in pratice:

    inline std::size_t get_significant_uint64_count_uint(
        const std::uint64_t *value, std::size_t uint64_count)
    {
        value += uint64_count - 1;
        for (; uint64_count && !*value; uint64_count--)
        {
            value--;
        }

        return uint64_count;
    }

It looks like it modifies value but also uint64_count. I couldn't find what && does but I guess here it's for "AND". Anyways, it looks like the for loop runs until uint64_count>0 and *value!=0. But I don't get the logic.

Function is from here

Rafaelo
  • 33
  • 1
  • 15

3 Answers3

2

It looks like it modifies value but also uint64_count

Yes.

I couldn't find what && does

Really? Any decent C++ reference/book should cover logical operators, in this case logical AND.

I guess here it's for "AND"

Yes.

it looks like the for loop runs until uint64_count>0 and *value!=0.

No. It runs until either uint64_count is 0, or *value is not 0.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

Ugly code.

inline std::size_t get_significant_uint64_count_uint(
    const std::uint64_t *value, std::size_t uint64_count)
{
    value += uint64_count - 1;
    for (; uint64_count && !*value; uint64_count--)
    {
        value--;
    }

    return uint64_count;
}

Okay, remember that you're passing both arguments by value. So modifying value or count doesn't do anything outside this method. They're copies of the originals.

So, value is a pointer into memory. The code increments it by the count, then the code starts looking from that location back towards the beginning for a null byte.

So I guess it's trying to find the count of bytes to the final null in the data.

Why? No clue.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • 1
    "*looking ... for a null byte*" -> "*looking ... for a **non-null** byte*", actually. "*it's trying to find the count of bytes to the final null*" -> "*it's trying to find the count of bytes to the final **non-null***", since it loops while `*value` is 0, so it breaks when `*value` is not 0. – Remy Lebeau Sep 14 '21 at 21:02
0

The function runs from the end of an array pointed by value until finds a value of 0 in the array or reached the end of the array, and returns the index in the array of the value equals to 0.

user107511
  • 772
  • 3
  • 23
  • "*until finds a value of 0*" -> "*until it finds a **non-zero value***", actually. It loops while `*value` is 0, so it breaks when `*value` is not 0. – Remy Lebeau Sep 14 '21 at 21:00