Thread: Can this be done!

  1. #1
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124

    Can this be done!

    Away from a compiler right now but I was thinking about the following code. The overall result will be to print the bit reprsentation of input.

    Thx

    [code]

    struct n_bits {
    unsigned int a:1;
    unsigned int b:1;
    unsigned int ...
    unsigned int p:1;
    }nbits;

    nbits = getchar();
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not like that it can't - you would have to make the struct part of a union, with the other member of the union being an int.

    Besides, you're assuming a specific number of bits in a char/int/whatever

    It's much better to use a loop and CHAR_BIT from limits.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed