0

I am trying to create a uint8_t array and try to change the first element of the array, then print it to the terminal as a string.

But, after I assign currBlock (changed block) to arr[0], cout gives an error. I tried to find the answer in StackOverflow but couldn't find a similar question. Can you help me with it?

Error: bitset::_M_copy_from_ptr

#include <iostream>
#include <sstream>
#include <bitset>
    
int main()
{
    uint8_t arr[3]{0};
    uint8_t currBlock{arr[0]};
    int flag{1};
    currBlock ^= (-flag ^ arr[0]) & (1UL << 3);
    cout << "Buffer is : " << bitset<24>(arr).to_string() << endl;
    arr[0] = currBlock;
    cout << "Buffer is : " << bitset<24>(arr).to_string() << endl;
    return 0;
}

I was expecting a print out of the uint8_t, but instead I get an error.

8
  • 3
    You should usually share the error message you received, as it can provide many clues as to what is happening. Commented Jan 31, 2023 at 0:20
  • 1
    I got an error First step is to read the error message. Commented Jan 31, 2023 at 0:23
  • 1
    Is this the error? Commented Jan 31, 2023 at 0:24
  • 4
    Runtime error. Bitset is expecting a null-terminated string of 1s and 0s. You're not giving it one. Even before reading the error message you should read the documentation Commented Jan 31, 2023 at 0:25
  • There is no quick and easy way to do this, though I think there should be, so here's some suggestions ho how to get this done right. Commented Jan 31, 2023 at 1:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.