checking for bytes

Started by
6 comments, last by Zahlman 13 years, 2 months ago
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
Advertisement
http://www.cplusplus.com/reference/clibrary/climits/

[font="Courier New"]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!

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


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

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!

'nuclear123' said:

[size="1"]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<blockquote class="ipsQuote" data-ipsquote data-ipsquote-contentapp="forums" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contenttype="forums" data-ipsquote-username="wikipedia">In the programming languages <a href='&quot;http://en.wikipedia.org/wiki/C_programming_language&quot;'>C</a> and <a href='&quot;http://en.wikipedia.org/wiki/C%2B%2B&quot;'>C++</a>, the <a href='&quot;http://en.wikipedia.org/wiki/Unary_operator&quot;'>unary operator</a> &#39;<span style="font-weight:bold;">sizeof&#39;</span> is used to calculate the sizes of <a href='&quot;http://en.wikipedia.org/wiki/Data_type&quot;'>datatypes</a>, in number of bytes</blockquote>
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.

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.

This topic is closed to new replies.

Advertisement