Thread: Characters into bitwise ints

  1. #1
    Code Zer0
    Guest

    Question Characters into bitwise ints

    Is it possible to directly "pack" a character into a bitwise unsigned int?

    I tried checking the FAQ's and saw the operands listed for bitwise operations. I also saw the mention for packing integers into bitwise stuff, but I don't know if it is possible to do it with characters.

    Anyone care to shed some light on this for me? I'm just having a tough time trying to picture it how it's intended

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    a character is just a 1-byte integer (I recommend you use unsigned char for packing). you can pack characters in the exact same way you would any other integer type.

  3. #3
    Code Zer0
    Guest
    Originally posted by Polymorphic OOP
    a character is just a 1-byte integer (I recommend you use unsigned char for packing). you can pack characters in the exact same way you would any other integer type.
    So instead of an unsigned Integer, I would use an unsigned char?

    Also, in the C book I'm using (C: How to program | Deitel & Deitel ), it has a variable type under the bitwise stuff it uses for an example that is just "unsigned." With something like that, does that mean that it would "assume" integer variable type, or something else?

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Code Zer0
    With something like that, does that mean that it would "assume" integer variable type, or something else?
    Yes, "unsigned" means "unsigned int", just as "long" means "long int".
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Code Zer0
    Guest

    Talking Packin' em away?

    Okay, so basically how would you go about packing said characters?

    I'm assuming that each character is 8 bits long, and therefore to store 4 characters, I would need to declare the unsigned char to be 32 bits long, correct?

    Now, the packing stuff, I think I can figure out, but what about unpacking? the assignment calls for it to be done with the right-shift operand. Would that mean that basically I would write something like this to unpack?

    Code:
    a = storage >> 8;

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    One char is 8 bits
    One int is 32 bits

    You can fit 4 chars into one int. This can be accomplished a number of ways.

    Code:
    union {
      char x[3];
      int y;
    
    };
    x[0] = 0xAA;
    x[1] = 0xBB;
    ...
    You could shift...
    Code:
    char x[3];
    int y;
    y = x[0];
    y |= x[1] >> 8;
    y |= x[2] >> 16;
    y |= x[3] >> 24;
    I'm not sure about the direction of the shift operators, or what endian it's all stored in, but you can sort these things out.

  7. #7
    Code Zer0
    Guest

    Question

    So when you declare the unsigned char that will be receiving the characters, would I declare its size in the number of bits, or in the number of characters it will take? Obviously it will be an array-type situation, but the sizing is what I am a bit confused about in your example.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Sorry but i have a book that verifies an int 2 bytes or 16 bits giving it a range of 65535 and 0. 32 bits is enough bits to store a number up to just under 4.3 Trillion unsigned.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Sorry but i have a book that verifies an int 2 bytes or 16 bits giving it a range of 65535 and 0.
    The size of an int is implementation defined. It is therefore not always 2 bytes, nor is it always 4 bytes, but it is always sizeof(int) bytes.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Code Zer0
    Guest
    Originally posted by Hammer
    The size of an int is implementation defined. It is therefore not always 2 bytes, nor is it always 4 bytes, but it is always sizeof(int) bytes.
    So does the sizeof() return the amount in amount of bits or bytes?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. Mapping Characters to Ints
    By Keegan in forum C Programming
    Replies: 7
    Last Post: 11-16-2007, 04:57 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM