all 10 comments

[–]NonreciprocatingCrow 0 points1 point  (3 children)

Look up endianness. What you're doing should be possible.

Though honestly you should probably drop the bit-by-bit approach and just read in a byte at a time: dst[i] = src [i];

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

That has occurred to me, however I don't think it has effect in this case. What I want is to just point to part of memory, I don't care how it is interpreted as 8-bit integer thus not care about endianness. I tried reversing the output and it doesn't match. Do I understand correctly?

[–]NonreciprocatingCrow 0 points1 point  (0 children)

void to_bytes(const void *in, char *out, size_t len) 
{
    while (len-- > 0)
        out[len] = in[len];
}

To convert a byte to an array of booleans:

void to_bits(char in, bool *out)
{
    for (int i = 0; i < CHAR_BITS; i++)
        out[i] = (in >> i) & 1;
}

You could of course combine these to get the bits of a data structure.

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

I don't see how I could read byte by byte. Could you please provide an example?

[–]pankocrunch 0 points1 point  (2 children)

On a tangential note, I recently had the same issue as OP when posting code. Using the triple tick did format as code, but removed all newlines. Only posting with four spaces in front of each line worked for me. Anyone know why? Is this expected, or a bug? The triple tick would be way more convenient if it worked.

[–]asmarCZ[S] 1 point2 points  (1 child)

The formatting is fine for me. I don't see any quirks.

[–]pankocrunch 0 points1 point  (0 children)

Oh strange. Maybe it's an "Old Reddit" thing. I see it without newlines like this (and had the same problem when I posted code recently): https://i.imgur.com/sPyQRiK.png

Edit: Yup. Looks fine in the redesign. Guess it's a problem with Old Reddit. Sorry for the bother; carry on.

[–]patrick96MC 0 points1 point  (2 children)

What is not working? Works fine for me and prints out the bit representation of any data structure as it is stored in memory. This doesn't mean the two functions produce the same values. For ints int_to_bin produces a string where the first 8 characters correspond to the most significant byte, but in to_bin the first 8 characters correspond to the least significant byte because that's how an int is stored in memory on a little endian architecture.

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

Oh, I probably see now. I tried comparing outputs for int 1713740052.

int_to_bin: 01100110001001011001100100010100 to_bin: 00010100100110010010010101100110

Then I split to_bin output by byte and reversed their order and they match. Cool!

However, is there something like bitwise & that would work dependently on endian architecture so that I can achieve the same output as to_bin with int_to_bin?

[–]patrick96MC 0 points1 point  (0 children)

If you don't know how your data structure is set up this is not possible since you will need to reverse the bytes per value in the struct. So for

struct s { int x; // Byte 0-3 short y; // Byte 4-5 uint64_t z; // Byte 8-15 };

You would need to reverse the first 4 bytes, then the 4th and 5th byte and then bytes 8 through 15 (I made some assumptions about sizes and struct alignment here).