Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print bits from argv

I’m sure the answer is simple but I can’t get my head around so I hope you can help me 🙂

I’m trying to print the bits of a unsigned char, here with the value 2, the issue is that I get a wrong answer when casting from argv[1][0] and the good one when sending directly the number 2.

void    print_bits(unsigned char octet)
{
    int i;
    unsigned char   b;

    i = 8;
    while (i--)
    {
        b = (octet >> i & 1) + '0';
        write(1, &b, 1);
    }
}

int main(int argc, char **argv)
{
    print_bits((unsigned char)argv[1][0]);
    write(1, "\n", 1);
    print_bits(2);
    write(1, "\n", 1);
    return (0);
}

gives me :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

00110010
00000010

I notice when debugging that, if I print the "octet" variable at the start of print_bits, I get the ASCII value of 2 when casting from argv and the value "\002" when directly inputting the value 2.
It feels like a casting issue ?
Thank you!

>Solution :

char are just integers, when you input ‘2’ from argv, it reads the character ‘2’ (which is equal to 50 in ASCII). In your second case, you input the int 2, hence the two different outputs. Your function is correct and so is the output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading