checking for bytes
#1 Members - Reputation: 119
Posted 26 January 2011 - 07:52 PM
sizeof(variable) == sizeof(char[5])
is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
for example....a char is not always == to a byte...even tho on most compiler's it is....this can change in the future..my question is how can i check the a certain variable has 8 bits being used and is by definition a byte
#2 Members - Reputation: 1362
Posted 26 January 2011 - 08:13 PM
CHAR_BIT seems like the it would work. You'd want this, I think:
sizeof(variable) * CHAR_BIT == 5*8
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
#4 Members - Reputation: 840
Posted 26 January 2011 - 08:47 PM
is there anyways c++ actually allows messing with actual BIT's to do comparison on sizes? i feel this would be a more secure/safe/portable way -thx
Nope, don't think so. Bytes are the unit of memory. You can't get an address of a bit.
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game
#5 Members - Reputation: 355
Posted 27 January 2011 - 12:57 AM
#6 Members - Reputation: 54
Posted 27 January 2011 - 06:51 AM
Yes the literal 5how would i do this?
sizeof(variable) == sizeof(char[5])
is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
No a char is always a Language byte, how many bits this has is a different matter and one which CHAR_BIT will tell you.for example....a char is not always == to a byte...even tho on most compiler's it is....this can change in the future..my question is how can i check the a certain variable has 8 bits being used and is by definition a byte
#7 Moderators - Reputation: 14300
Posted 27 January 2011 - 07:11 AM
Yep, first thing wikipedia says is that sizeof is a number of bytes, so you want sizeof(variable) == 5Yes the literal 5
is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
In the programming languages C and C++, the unary operator 'sizeof' is used to calculate the sizes of datatypes, in number of bytes
#8 Members - Reputation: 2048
Posted 27 January 2011 - 07:54 AM
So... don't make your life complicated if you don't have to.
#9 Moderators - Reputation: 1666
Posted 27 January 2011 - 11:30 PM
how would i do this?
sizeof(variable) == sizeof(char[5])
is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?
Yes. It's called "5".
for example....a char is not always == to a byte...
Yes, it is. However, a byte is not always the same thing as an octet.
If a char has 64 bits in your environment, then a byte has 64 bits in your environment. As far as the language of the C++ standard is concerned, there is nothing about a byte that involves having exactly 8 bits in it.
even tho on most compiler's it is....this can change in the future..
It has to do with more than just your compiler.
my question is how can i check the a certain variable has 8 bits being used
A char must have at least 8 bits. It is also not allowed to have a type smaller than char. Every variable's size is made up of char-sized units. (sizeof(char) == 1, by definition).
The real question is, why do you care about the exact number of bits in use by any of your data types? Even if you knew and had a good reason to care, there is very little you can do about it at this high of a level of programming. Yes, for the exceedingly few people who have a reason to care about this sort of thing, C++ (or even C) is probably much too high-level.






