How can this be achieved?

 
struct atom{unsigned char hex[2]: 4;};


An ideas?
I want an array of 4 bits?
Yes! I'm currently using a lot. It is used to reduce construct (struct, class, onion) size data.

This can be calculated by this formula : (for 86x)
(Value) = size of all elements (bytes);
(Result) = sizeof(structure);

(Result) = (Value) + ((Value) % 4 == 0) ? 16 : 0)
(Result) % 4 == 0; //(Otherwise, (Result)++, recalculate this step
(Result) /= 4;

Edit :
struct atom{unsigned char hex[2]: 4;};
No unsigned char, only 'unsigned'
The correct code :
struct atom{unsigned hex[2] : 4;};
hex contains two values, each value has 4 allocated bytes. (= half of char)
But, maybe your idea (!??!?!) Because this feature currently is not supported.

Tip : I always use:
struct atom{unsigned char bBool : 1;}; //for bool values (8x size reduced)


Good question.
Last edited on
Disregard Jackson Marie. He doesn't make sense most of the time.

The smallest amount of memory is 1 byte.
You can manipulate the bits within that byte with the bit operators | & etc.

What you're looking for is probably this:

http://www.cplusplus.com/reference/stl/bitset/
Jackson's code doesn't work! :(

Thanks coder777, still, I would appreciate a C code
Or either an idea, of how I could manipulate using bit operators!
Sorry SameerThigale....
I said "maybe this feature currently is not supported" - I doubt, however, I always convert the original variables to simple or low-size variable to save memory. I now haven't found out the code that is used to initiate the array of a custom-size variable, so I'm not sure. It's the best tweaking method for structures and in fact I want to learn more how to define an array of a custom-size variable.
And, sorry SameerThigale again for my bad answer...
Last edited on
@Jackson Marie
you didn't get the bit operations at all.

Unfortunately they're not easy to explain. Look at this:

http://cplusplus.com/doc/tutorial/operators/

for what type of bit operators exists.


Ok, what does this do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct atom
{
  unsigned char x : 4; // This are the lower 4 bits in a byte
  unsigned char y : 4; // This are the higher 4 bits in the same byte
  unsigned char z : 4; // This are the lower 4 bits in a second byte
  // it is possible to use other types like unsigned int which would lead to [4] usable bytes
};

// You can use it as follows
atom a;
a.x = 5; // The code for this would be: byte value = 5 | (y & 0xf0)
// what would happen if a.x = 20? Well: it exceeds the 4 bit capacity and would overwrite y!
a.y = 5; // The code for this would be: byte value  = (5 << 4) | x
// the byte value: 0x55

int b = a.x // resolves to: byte value & 0x0f
int c = a.y // resolves to: byte value >> 4 


if you write the hex value 0xf0 as binary it would be 11110000
The point is that c/c++ knows 3 number system: decimal, hexadecimal, and (rarely used) octal.
But unfortunately not binary. So if you want to manipulate a certain bit (like the third: 00000100) you have to convert it. Using the calculator: The third bit has the value 4.

The | does the following 1 | 0 = 1 (this is just 1 bit, but it can be use for multiple bits)
The & does the following 1 & 0 = 0

The << shifts the bits (5 -> 101) x (four in this case) times left -> (bin: 1010000 = dec: 80 = hex: 50)
The >> shifts the bits x (four) times right


hope the explanation helped somewhat (and i didn't make a mistake somwhere).
I suggest that (both) you check that out and play with some values to get the feeling for that

Thist may help:
http://en.wikipedia.org/wiki/Bitwise_operation
Last edited on
Topic archived. No new replies allowed.