to calculate bitsize of a byte

J

Jordan Abel

Hmm.. But what about a machine with 9 bit bytes where CHAR_BIT is 8
(the C compiler simply ignores the most significant bit)? I believe
that the standard doesn't allow padding bits in char but I think in
this case the most significant bit is not a padding bit, it is simply
ignored by the compiler author. In this case a string of chars will
still be contiguous from the point of view of C but you won't be able
to ever find out the size of the machine byte from C.

If it's "ignored", it has to be consistently ignored - i.e. it is a
padding bit in both [or more] bytes in an int, and in all [at least]
four bytes in a long. And it can't be a "dangerous" padding bit [i.e. if
assignments to it as unsigned char sets or clears it, having it
set/clear can't be a trap representation of other types or vice versa].
I'm not so sure about how conformant is such an implementation. Any
standard experts here think it's valid?

If it's _impossible_ to prove in software that such a bit exists, I
suppose the implementation could be valid. I suppose one could say that
the ECC bit is a real-world example.
 
V

vy0123

Michael said:
8 - - -

Given that, try this code:

#include <stdio.h>
int main(void)
{
unsigned i, c;
unsigned char *bp1, *bp2;

if (sizeof i > 1)
{
i = 0;
bp1 = bp2 = (unsigned char *)&i;
bp1++; /* point to second byte */ #if 0
bp2 += sizeof i - 1; /* point to second-to-last byte */
#else
bp2 += sizeof i - 2; /* correction: point to second-to-last
byte*/
#endif
for (c = 0; *bp1 == 0 && *bp2 == 0; c++)
i = 1u << c;
printf("bytes have %d bits in this implementation\n", (int)c - 1);
}
return 0;
}

This works for both big- and little-endian architectures, as far as I
can see, because it tests when the shift operator alters the second
or penultimate bytes.

Works now.
 
C

CBFalconer

.... snip ...

Hmm.. But what about a machine with 9 bit bytes where CHAR_BIT is 8
(the C compiler simply ignores the most significant bit)? I believe
that the standard doesn't allow padding bits in char but I think in
this case the most significant bit is not a padding bit, it is simply
ignored by the compiler author. In this case a string of chars will
still be contiguous from the point of view of C but you won't be able
to ever find out the size of the machine byte from C.

I'm not so sure about how conformant is such an implementation. Any
standard experts here think it's valid?

You are talking about machines with parity memory, for example.
ECC memory machines just organize things somewhat differently, but
can be built around parity memory. However parity memory cannot
necessarily be built from ECC modules.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
R

Richard G. Riley

#include <stdio.h>
int main(void)
{
unsigned short i,j;
/* i and j must be the same type needed */
/*
unsigned long i,j;
char i,j;
*/

unsigned short k;
for(i=1,j=0,k=0;i!=j;k++,j=i,i<<=1);
k--;
printf("\nbits %d\n",k);
return(0);
}


How is this superior to the previous bit shifting example?

e.g
int numbits=1; /* assuming c unsigned */
while(c<<=1)
numBits++
 
R

Rod Pemberton

Richard G. Riley said:
How is this superior to the previous bit shifting example?

I never said it was. There are probably a thousand ways to do this. Some,
I'm sure, are much better than mine. Mine does fix one potential error
though. The first example assumes the the data will wrap to zero (0) when
the bit shift overflows. This may not be the case. Mine just checks for
equality. If the bit size of the register or memory location is larger than
the CHAR_BIT size, the first example will fail. Also, certain CPUs don't
have bit shifts but have bit rotates. If this is the case and the compiler
uses them to emulate bit shifts, the first example might fail too.

Rod Pemberton
 
A

Andrey Tarasevich

Rod said:
...
An alternate method that should work for ones and twos complement integers:

#include <stdio.h>
int main(void)
{
unsigned short i,j;
/* i and j must be the same type needed */
/*
unsigned long i,j;
char i,j;
*/

unsigned short k;
for(i=1,j=0,k=0;i!=j;k++,j=i,i<<=1);
k--;
printf("\nbits %d\n",k);
return(0);
}
...

On a pedantic side, all these methods attempt to calculate the bit-size of
_value_ _representation_ of certain type, which (theoretically) could be less
than the bit-size of its _object_ _representation_, because the latter might
include extra padding bits. It applies to all types except '[signed/unsigned]
char'.

I don't know which representation the OP was interested in, but in case of a
"byte" it doesn't really matter, since "byte" is synonymous with 'char' type in
standard C terminology.

Once you start dealing with "larger" types (as is the case in your code), the
ambiguity immediately presents itself.
 
A

Andrey Tarasevich

Michael said:
...

I don't think this is true. Consider that:

- integers must have a pure binary representation, which means
value bits are in order

What order? At the language level the "bit" and the "order of bits" are just
concepts. The only way to interact with "bits" and analyze their "order" is to
use bitwise or shift operators, which are also language-level constructs. There
are absolutely no requirement for the language-level "order of bits" to be in
any agreement with machine-level order of bits. Moreover, there is no
requirement for the machine to even have binary "bits". The machine itself might
be ternary. This still does not prevent the implementation from using the "pure
binary representation" (as seen by bitwise and shift operators) for representing
integers.
- you can inspect the representation of any object using a
pointer to unsigned char

You will only see the language-level representation of the type 'unsigned
char', which, in general case, has absolutely nothing to to with the
machine-level details.
Given that, try this code:

#include <stdio.h>
int main(void)
{
unsigned i, c;
unsigned char *bp1, *bp2;

if (sizeof i > 1)
{
i = 0;
bp1 = bp2 = (unsigned char *)&i;
bp1++; /* point to second byte */
bp2 += sizeof i - 1; /* point to second-to-last byte */
for (c = 0; *bp1 == 0 && *bp2 == 0; c++)
i = 1u << c;
printf("bytes have %d bits in this implementation\n", (int)c - 1);
}
return 0;
}

This works for both big- and little-endian architectures, as far as I
can see, because it tests when the shift operator alters the second
or penultimate bytes. I don't offhand see a way for a conforming
implementation (where sizeof(unsigned) > 1) to produce the wrong
answer.
...

This might work "in most (or all) cases" in practice, but it is still not
something with language-guaranteed behavior. Object 'i' is allowed to contain
padding bits, which might happen to occupy both bytes you are trying to analyze
in the above code and might happen to stay 0 for all legal values of 'i'. I.e
any attempts to modify 'i' might never change the bytes in question, thus
resulting in never-ending cycle. This is unlikely in practice, but perfectly
legal from the language point of view.
 
M

Michael Wojcik

[To determine the number of bits in a byte:]
Define an unsigned char with a value of -1,
and count the right shifts until zero.

Heh. That's a lot simpler than my method. I was so busy trying to
portably handle overflowing left shifts I didn't even consider right
shifts.
 
D

david ullua

Vladimir S. Oka 写é“:
Oh, alright...

#include <limits.h>

int bytesize = 1 * CHAR_BIT;

Does this qualify as calculation?

If you have a compliant Standard C implementation this has to work, and
it won't fail you.

There are batter ways of testing algorithmic skills in an interview
than contriving something like this.

PS
Please quote what you're replying to. Many people can't see the whole
thread.

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Hi,Vladimir, Thanks for your PS. I would never do that again.
 
D

david ullua

Default User 写é“:
Both who? See below.



Brian

De, ...hmmm, could I address you "Default User"?
I give my thanks to both Vladimir and Martin.
Your signature(?) is short and clear, Thank you!
 
D

david ullua

Emmanuel Delahaye 写é“:
david ullua a écrit :

/Your/ C-book needs to be read from cover to cover. every 6 month.

good idea, it's time to learn it again.
 
D

david ullua

Hi, everyone, thanks for all of your passionate reply. Since i am a
green hand here, lots of people tell me how to reply. I am terribly
sorry that I didn't see and those remarks until today cause busy the
job.

It's a better group than any group I've ever joined. There are so many
smart and learned here.
Hope everyone would enjoy it :)
 
C

CBFalconer

david said:
Hi, everyone, thanks for all of your passionate reply. Since i
am a green hand here, lots of people tell me how to reply. I am
terribly sorry that I didn't see and those remarks until today
cause busy the job.

It's a better group than any group I've ever joined. There are
so many smart and learned here.

Here are some useful links. Read them.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
P

pete

david ullua wrote:
It's a better group than any group I've ever joined.

Believe it or not,
it's largely due to the fact that
we gang up and mob off topic posters.
 
D

Default User

david said:
Default User 写é“:



De, ...hmmm, could I address you "Default User"?

What do you think?
I give my thanks to both Vladimir and Martin.
Your signature(?) is short and clear, Thank you!


You're welcome.




Brian
 
D

Default User

pete said:
Believe it or not,
it's largely due to the fact that
we gang up and mob off topic posters.


Is that why we do it? I thought it was because we were evil control
freaks who hate newbies and outsiders.



Brian
 
C

CBFalconer

Default said:
Is that why we do it? I thought it was because we were evil
control freaks who hate newbies and outsiders.

Don't forget the inner satisfaction gained by restricting knowledge
from the unwashed. Oooh, it's positively orgasmic.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

MrG{DRGN}

Default User said:
Is that why we do it? I thought it was because we were evil control
freaks who hate newbies and outsiders.


Hey, I'm a newbie, and I don't feel hated. You guys must be slacking off.
heheh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top