Jump to content

  • Log In with Google      Sign In   
  • Create Account


checking for bytes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 nuclear123   Members   -  Reputation: 119

Like
0Likes
Like

Posted 26 January 2011 - 07:52 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?
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

Sponsor:

#2 nobodynews   Crossbones+   -  Reputation: 1855

Like
1Likes
Like

Posted 26 January 2011 - 08:13 PM

http://www.cplusplus.com/reference/clibrary/climits/

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!


#3 nuclear123   Members   -  Reputation: 119

Like
0Likes
Like

Posted 26 January 2011 - 08:41 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



#4 karwosts   Members   -  Reputation: 836

Like
0Likes
Like

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.
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#5 SiS-Shadowman   Members   -  Reputation: 359

Like
0Likes
Like

Posted 27 January 2011 - 12:57 AM

From the wiki entry to stdint I gather that you can rely on the uintN_t types to be integers that hold exactly N bytes. Thus sizeof(5 * uint8_t) should do the job. However you need to verify if the platforms you require support at least this part of the C99 standard.

#6 magic_man   Members   -  Reputation: 54

Like
0Likes
Like

Posted 27 January 2011 - 06:51 AM

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 the literal 5 :)

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

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.



"You insulted me!" I did not say that in the private message Tom Sloper!

#7 Hodgman   Moderators   -  Reputation: 29725

Like
0Likes
Like

Posted 27 January 2011 - 07:11 AM


is there a replacement i could use for sizeof(char[5]) which would signify 5 bytes?

Yes the literal 5 :)

Yep, first thing wikipedia says is that sizeof is a number of bytes, so you want sizeof(variable) == 5

In the programming languages C and C++, the unary operator 'sizeof' is used to calculate the sizes of datatypes, in number of bytes



#8 samoth   Crossbones+   -  Reputation: 4718

Like
0Likes
Like

Posted 27 January 2011 - 07:54 AM

Why would one care anyway? Unless you write something very, very, very specific, you should not need to worry. First, it is not likely for you to ever encounter a non-8 bit system, and even so... if bytes are not 8 bits, they still are "the unit", a char is still a byte, and the size of any other POD or struct is still a multiple of char.

So... don't make your life complicated if you don't have to.

#9 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS