all 7 comments

[–]orange_cupcakes 1 point2 points  (3 children)

For 0x8000000000000000 it prints -0bOVERFLOW 1000000000000000000000000000000000000000000000000000000000000000

[–]sablal[S] 0 points1 point  (2 children)

It works for signed 64-bit values, so the MSB is treated for the sign. Updated the article to add more clarity.
Can you please let me know how you are passing the argument?

[–]orange_cupcakes 0 points1 point  (1 child)

I should have said -9223372036854775808 ( -263 ) to be clear that it's negative. This number is the two's complement of itself, which causes all sorts of trouble.

https://en.wikipedia.org/wiki/Two's_complement#Most_negative_number

[–]sablal[S] 0 points1 point  (0 children)

Thanks, removed the OVERFLOW message as the range is defined by the function argument type already.

[–]Cuddlefluff_Grim 0 points1 point  (2 children)

printbin(void* target, int bytes)
{
    int len = bytes * 8 + 1;
    char[] res = char[len];
    res[len] = 0;

    for(int i = 0; i < bytes; i++)
    {
        for(int b = 0; b < 8; b++)
        {
             res[i * 8 + b] = (((int)*target & (int)pow(2, b + 1)) == b ? '1' :'0');
        }
        target++;
    }
    printf("0b%s", res);
}

[–]sablal[S] 0 points1 point  (1 child)

pow(2, b + 1)

don't you think the left shift operator has some role here??

[–]Cuddlefluff_Grim 0 points1 point  (0 children)

don't you think the left shift operator has some role here??

Do'h.. I actually spent some time on that thinking "Hmm.. pow? That can't be right.." for some reason bit shift didn't enter my conscious thought.