I am trying to use the LSB search method proposed by Andrei Grant in answer to this question: Position of the minimum significant bit set
However, this leads to a segmentation error. Here is a small program demonstrating the problem:
#include <iostream> typedef unsigned char Byte; int main() { int value = 300; Byte* byteArray = (Byte*)value; if (byteArray[0] > 0) { std::cout<< "This line is never reached. Trying to access the array index results in a seg-fault." << std::endl; } return 0; }
What am I doing wrong?
I read that it is not recommended to use "C-Style" in C ++. Should I use reinterpret_cast<Byte*>(value) instead? However, this still leads to a segmentation error.
source share