Thread: Characters into bitwise ints

1. 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. 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. 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. 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".

5. 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. 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. 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. 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.

9. >>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.

10. 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