0

This is my code:

template <typename T> void printlnbits(T v) {
    
    const int value_size = sizeof(v) * 8;

    long long int* ch = (long long int*)&v;

    int j = 0;

    for (int i = value_size - 1; i >= 0; --i)
    {
        extractBit(*ch, i);

        j++;

        if (j == 8)
        {
            std::cout << " ";
            j = 0;
        }
    }

    std::cout << "\t" << value_size << std::endl;
}

and

void extractBit(long long int ch, int ith) {
    std::cout << (ch & (1 << ith) ? 1 : 0);
}

When passing in the argument: const long long unsigned e = 1LLU << 40; the output is:

10000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000 64

Now 1LLU << 40 = 1099511627776

Should'nt the output be:

00000000 00000000 00000000 10000000 00000000 00000000 00000000 00000000 64

Am I missing something?

Struct as argument:

When I am sending in a struct as argument, how does the calculation of the variables inside the struct get calculated together and store a value which then gets outputed as a bit representation?

struct foo {
    int a = 2;
    char b = -1;
    unsigned long long int x = 1LLU << 63;
};

outputs:

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11001100 11001100 11001100 11111111 00000000 00000000 00000000 00000010 128

Leon
  • 63
  • 7
  • 4
    try `1LLU << ith` – VainMan Sep 29 '21 at 11:30
  • @VainMan yeah that solved it! Thx – Leon Sep 29 '21 at 11:39
  • When I am sending in a struct as argument (see the edited question with the struct), how does the calculation of the variables inside the struct get calculated together and present a value which then gets outputed as a bit representation? – Leon Sep 29 '21 at 11:53
  • duplicate: [How do I bit shift a long by more than 32 bits?](https://stackoverflow.com/q/2404439/995714), [bit shifting with unsigned long type produces wrong results](https://stackoverflow.com/q/31744305/995714) – phuclv Sep 29 '21 at 12:18
  • @phuclv I mean more like, when sending in a struct (see the struct in the question posted) as argument to the template, the value v will hold: a = 2, b = 1 and x = 9223372036854775808 (because x is unsigned long long int x = 1LLU << 63;). These three variables will somehow get calculated and get the value 14757395474574999554. I want to know how the calculation of those variables lead to that value. The value above will then be printed to the concole as a bit representation for the entire struct (see the bit representation in the question). – Leon Sep 29 '21 at 12:37
  • the issue lies in this line `std::cout << (ch & (1 << ith) ? 1 : 0);`. How is it related to the struct? Besides you're violating the strict aliasing rule when doing this `(long long int*)&v` which invokes undefined behavior – phuclv Sep 29 '21 at 12:40
  • @phuclv, I changed this `std::cout << (ch & (1 << ith) ? 1 : 0);` to this `std::cout << (ch & (1LLU << ith) ? 1 : 0);` ch will hold the value 14757395474574999554. I have also tried using the intptr as `intptr_t* ptr = reinterpret_cast(&v)` instead of `(long long int*)&v` but then the output gets totally wrong.. I mean how can a + b + x = 14757395474574999554? – Leon Sep 29 '21 at 12:46
  • Big Endian or Little Endian? – Thomas Matthews Sep 29 '21 at 14:44

0 Answers0