0

I know this initialization: bitset<20>.

But what does this initialization mean: bitset<1<<20>?

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 2
    `1<<20` means 2 raised to power 20 or as @Quimbly said 1 left shifted by 20 bits – kuro Oct 18 '22 at 06:59
  • `1<<20` is a number, and is in the same position as `20` in the first example. I'm guessing that you haven't encountered the shift operator `<<` in its original habitat before. – molbdnilo Oct 18 '22 at 07:32
  • See [C++: what does (a< – Jason Oct 18 '22 at 10:28

1 Answers1

1

But what this initialization means?

Parsing headache for the compiler perhaps.

You can use equivalent snippets: bitset< 1<<20 > - i.e. 1 left-shifted by 20 bits - bitset<1048576>

Quimby
  • 17,735
  • 4
  • 35
  • 55
  • Pedantically saving 1 character to type. – Casey Oct 18 '22 at 07:10
  • 4
    `1<<20` shows more clearly your intent to future devs; it's not pedantic. – tenfour Oct 18 '22 at 07:17
  • @Casey what is clearer? `1<<20` or `1048576`. In fact spaces should always be padded around binary operators so it should be `1 << 20` so it'll be longer – phuclv Oct 18 '22 at 10:26
  • 1
    @phuclv Since the values are known at compile time, it's more clear to give `1048576` or `1<<20` a *name* via a `constexpr` variable rather than being clever with bit shifting. – Casey Oct 18 '22 at 18:09
  • @tenfour See previous – Casey Oct 18 '22 at 18:09
  • `bitset<(1 << 20)>`? – Evg Oct 18 '22 at 20:19
  • @Casey no, regardless of the name, you always have to use the clearer way. `1048576` won't be allowed to pass code review instead of `0x10'0000` or `1 << 20` – phuclv Oct 19 '22 at 13:06