<p>It's creating a string where each character value is composed of bits representing 8 booleans from m_foreign_values packed together.</p>

CC BY-SA 4.0
2022-09-15T22:34:06.603Z
2022-09-15T22:34:06.603Z
Pieter Wuille
<p>I'm studying for the upcoming pr review club.</p>
<pre><code>        if (!m_foreign_outputs.empty()) {
             std::string s(m_foreign_outputs.size(), 0);
             for (auto i = m_foreign_outputs.size(); i--; ) {
                 if (m_foreign_outputs.at(i)) {
                     s[i / 8] |= 1 &lt;&lt; (i % 8);
                 }
             }
             mapValueCopy[&quot;fout&quot;] = s;
         }
</code></pre>
<p>What is that for loop doing? and how is it not stuck forever?
Also what is happening here:</p>
<p><code>s[i / 8] |= 1 &lt;&lt; (i % 8);</code></p>
<p>Thanks!</p>

CC BY-SA 4.0
2022-09-12T00:30:28.150Z
I understand the loop now, I haven't seen that before, I guess it walks through the vector from the highest index to the lowest and exits the loop after zero. That's cool. I'm lost on the string, it looks like it is creating some sort of unicode values?
Kaizen
CC BY-SA 4.0
2022-09-12T21:10:25.777Z
Thank you, I guessed that when I woke up today but definitely wasn't sure.
Kaizen
CC BY-SA 4.0
2022-09-15T22:34:34.440Z
I’m voting to close this question because it's better asked in the review club, or on the PR.
Pieter Wuille
CC BY-SA 4.0
2022-09-15T22:36:51.783Z
I'm voting to close this topic, because it's a question about the C++ programming language.
Murch
<bitcoin-core-development><c++>
Help explaining some C++ code I haven't seen before
Kaizen
# Help explaining some C++ code I haven't seen before

(Asked by: username_0 on 2022-09-11)

I'm studying for the upcoming pr review club.

```
        if (!m_foreign_outputs.empty()) {
             std::string s(m_foreign_outputs.size(), 0);
             for (auto i = m_foreign_outputs.size(); i--; ) {
                 if (m_foreign_outputs.at(i)) {
                     s[i / 8] |= 1 << (i % 8);
                 }
             }
             mapValueCopy["fout"] = s;
         }

```

What is that for loop doing? and how is it not stuck forever?
Also what is happening here:

`s[i / 8] |= 1 << (i % 8);`

Thanks!

**Tags:** bitcoin-core-development, c++

> **Comments:**
> **username_0**: I understand the loop now, I haven't seen that before, I guess it walks through the vector from the highest index to the lowest and exits the loop after zero. That's cool. I'm lost on the string, it looks like it is creating some sort of unicode values?  
> **username_0**: Thank you, I guessed that when I woke up today but definitely wasn't sure.  
> **username_1**: I’m voting to close this question because it's better asked in the review club, or on the PR.  
> **username_2**: I'm voting to close this topic, because it's a question about the C++ programming language.  

---

## 1 answers

### Answer by username_1 on 2022-09-15. Score: 0

It's creating a string where each character value is composed of bits representing 8 booleans from m\_foreign\_values packed together.


---

