|
In VC++ can we make variable of less than a byte, something like store data of 4 bits?
--
"Programming is an art that fights back!"
|
|
|
|
|
I dont think so.May be i am wrong but as per i know it is not possible.
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
you cannot for individual variable, but using structure bitfields you can specify the size of struct member variable in bits.
struct Date {
unsigned short nWeekDay : 3;
unsigned short nMonthDay : 6;
unsigned short nMonth : 5;
unsigned short nYear : 8;
};
modified on Friday, May 23, 2008 3:14 AM
|
|
|
|
|
Wow that's gr8
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
thats good.
but here what is the size ofthe structure?
my doubt is unsigned short is 16 bits.
but the items of the structure demands 22 bits.
so ifthe structure sizeis 32 bits (2 shorts)
can you explain howthe memory is allocated to the items?
--------------------------------------------
Suggestion to the members:
Please prefix your main thread subject with [SOLVED] if it is solved.
thanks.
chandu.
|
|
|
|
|
chandu004 wrote: but here what is the size ofthe structure?
my doubt is unsigned short is 16 bits.
but the items of the structure demands 22 bits.
so ifthe structure sizeis 32 bits (2 shorts)
can you explain howthe memory is allocated to the items?
A bit clearer, please.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Click here ->[^]
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
i know the concept of bit fields, my dearfriend!
struct Date
{
unsigned short nWeekDay : 3; // 0..7 (3 bits)
unsigned short nMonthDay : 6; // 0..31 (6 bits)
unsigned short nMonth : 5; // 0..12 (5 bits)
unsigned short nYear : 8; // 0..100 (8 bits)
};
based on the above example,
nweekday+nMonthDay+nMonth occupies 3+6+5=14 bits,
so 2 bits are left over in the first short.
but my doubt is,
will nYear usethese two bits and remaining 6 bitsform other short or not?
|
|
|
|
|
It seems you know the answer and just put as question, if my arrogant assumption is correct, why don't you share the knowledge instead, if not see below,
chandu004 wrote: will nYear usethese two bits and remaining 6 bitsform other short or not?
No, Bit fields that cross the type boundary will skip bits to align the bitfield to the next type alignment, where type is the type of bitfield member. so, since nYear requires 8 bits and doesnot fit previous type (unsigned short - 16 bit) boundary, it skips to next 16 bit (unsigned short (type of nYear)) boundary. (PS: types other than int, like char, bool, short are not supported by every compiler.)
|
|
|
|
|
See #pragma pack()
You can use it to modify the default memory alignment for the compiler, it's argument is the number of bytes, the only valid values are 1,2,4,8,16.
|
|
|
|
|
you can specify the size in bits of each individual member, but still the total size in bits is less than 8, the instanciated structure will be 1 Byte minimum !!!
|
|
|
|
|
toxcct wrote: the instanciated structure will be 1 Byte minimum !!!
nope ! minimum size of storage unit depends on machine word length.
The Standard says that fields are packed into ‘storage units’, which are typically machine words. The packing order, and whether or not a bitfield may cross a storage unit boundary, are implementation defined. To force alignment to a storage unit boundary, a zero width field is used before the one that you want to have aligned.
struct MyOneBitStruct {
unsigned nOneBitMember : 1;
};
size_t nOneBitStructSize = sizeof(MyOneBitStruct);
run and check the value of nOneBitStructSize, as per your statement it should be sizeof(BYTE) (bitfields other than integer type is not supported by every compiler)
Offcourse, hardware does access to the complete word or individual Byte or individual bit depends on capability, but as far as the variable is language specific (Software part) accessing the variable is managed to the number bits specified in bitfields that is internally managed by compiler. And OPs question variable of less than byte size is answered by bitfields.
|
|
|
|
|
|
rp_suman wrote: can we make variable of less than a byte
no way.
actually, you could if your system could address bits, but that's not the case.
I believe some embedded electrical RAM for very specific needs can do that, but not that is into common computers though...
so even you use tricks to point to a particuliar bit (the struct tip explained in another post in this thread), the minimum amount of memory reserved will be a full Byte...
|
|
|
|
|
Thanks a lot for great help from everybody!!
Thanks & Regards,
Suman
--
"Programming is an art that fights back!"
|
|
|
|
|
How to convert CString to char* in vc++ 6 platform
Trioum
|
|
|
|
|
If unicode is not enabled this is done automatically (CString has a LPCTSTR). If UNICODE is enabled, then you should perhaps revise what you are doing and use TCHAR instead of char*. For which exact reason do you want the conversion ?
|
|
|
|
|
actually I want to getvalue from editcontrol in char* format
also set the value from char* to the editcontrol.
Trioum
|
|
|
|
|
Why a char pointer ? If you are using functions like SetWindowText and GetWindowText, they accept a LPCTSTR too. I really suggest that you take a look at this article[^]. It's a bit of reading but after it you'll definitevly know what you are doing and you will avoid any future problem. So, definitively a must read .
|
|
|
|
|
Well I was just going to point him to that article. 5 for beating me to it.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
I don't know why, but an idiot is down voting every good posts in this thread.
come to my help bro
|
|
|
|
|
Done.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Some sh*t-head down-voted almost all the messages on my profile page. Not a very good thing, I'd say.
|
|
|
|
|
let me check that
[edit] where ? I can't really found 1 votes on your profile... [/edit]
|
|
|
|
|
Well the above is not completely correct: since, as you pointed out, CString has the LPCTSTR operator, (when _UNICODE is not defined) you may cast it to a const char * pointer. If the OP really needs a char * then he has to do more (not excluding the Mark's favourite, (in)famous, CString::GetBuffer )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|