hi. I want new data type. because it isn't 18 byte. help...
Printable View
hi. I want new data type. because it isn't 18 byte. help...
You can now declare an instance:Code:typedef char newtype[18];
Code:newtype example="123456789012345678";
<sarcasm>create a struct with an 18-element array of chars</sarcasm>
your question, well you didnt ask a question, your post is extremely vague. because of that you will get vague responses. try again, telling us your detailed question.
Unless you have a 36-bit machine, you won't get an _integral_ type of exactly 18 bits. You can just approximate. Either you take 16 bits or 32. Or use a struct with a bitfield.
Code:typedef struct {
unsigned long bits : 18;
} ustruct18;
I sorry. Frankly, I have 8 bits mikrocontroller. unsigned long is 32 bits and 4 byte. this is show much. I need 18 bits. I trying " Brafil " code. code is false. tanks...
Put the "unsigned long bits : 18;" outside the struct to create to a 18 bit variable.
Structs can be padded by the compiler and while you can disable it, it's compiler dependent.
Not sure what you mean here; Brafil's code is letter-perfect. It creates a new type called "ustruct18" that you can use to declare variables, like so:
Code:typedef struct {
unsigned long bits : 18;
} ustruct18;
int main(void) {
ustruct18 bob;
bob.bits = 5;
return 0;
}
Then with following declaration in place
Code:unsigned long bits : 18;
Is the following assignment okay?
bits = 3FFFF; //Will set all the 18 bits.
ThanksCode:incase:
if i need only three bits enable for the following statement
unsigned int bit :5;
Is this assignment correct if i have to set 3 bits?
bit=0x07
well that case
Code:typedef struct {
unsigned int type : 5;
unsigned int bit:3;
} ustruct;
ustruct bye;
Is this assignment correct if i have to set 3 bits?
bye.bit=0x07;
And if you union your bits structure with an integer, you will find that setting those three bits will not necessarily set the least significant bits. It MAY do that, but by no means guaranteed. The only way to GUARANTEE bit-order is to use masks and shifts.
--
Mats
Well, okay, they're the three bits that are worth the least that are accessible via the bitfield (which may or may not have any relationship with the underlying integer). If we (that is, the OP) care about the underlying integer/register then using a bitfield is the wrong thing to do (which is probably your point).