473,573 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

to calculate bitsize of a byte

I am reading "Joel on Software" these days, and am in stuck with the
question of "how to calculate bitsize of a byte" which is listed as one
of the basic interview questions in Joel's book. Anyone could give some
ideas?I am expecting your reply.
David.

Feb 21 '06 #1
96 4893
david ullua wrote:
I am reading "Joel on Software" these days, and am in stuck with the
question of "how to calculate bitsize of a byte" which is listed as
one of the basic interview questions in Joel's book. Anyone could give
some ideas?I am expecting your reply.
David.


That's easy:

#include <limits.h>

int bitsize = CHAR_BIT;

--
BR, Vladimir

What is love but a second-hand emotion?
-- Tina Turner

Feb 21 '06 #2
david ullua wrote:
I am reading "Joel on Software" these days, and am in stuck with the
question of "how to calculate bitsize of a byte" which is listed as one
of the basic interview questions in Joel's book. Anyone could give some
ideas?I am expecting your reply.


The terms 'byte' and 'char' refer to (almost) the same thing in C.[1]
The predefined macro CHAR_BIT contains the size in bits of a char, and
therefore of a byte. The computation of the bitsize of a byte is simply
/* ... other code ... */
CHAR_BIT;
/* ... other code ... */
That wasn't so hard, was it?

[1] Some pedants will tell you that a 'char' is an object of the type
'char', while a 'byte' is the storage taken by such an object. It is
doubtful that such questions will have any real world impact on you.
Feb 21 '06 #3
Thanks for both of your reply.
I take a look at limits.h and other head files, there is really
something need to be read.
Before i knew CHAR_BIT is a defined value, I wrote the following
snippet to do the job:
(now i realize it is not neccessary)
char c = '\01';
int i=0;
do
{
i++;
printf("%d:%x(d ex)\n",i,c);
c = c<<1;
}while(c>0);
printf("bit count in a byte:%d",i);

Feb 21 '06 #4

"david ullua" <da*********@gm ail.com> wrote in message
news:11******** ************@g1 4g2000cwa.googl egroups.com...
I am reading "Joel on Software" these days, and am in stuck with the
question of "how to calculate bitsize of a byte" which is listed as one
of the basic interview questions in Joel's book. Anyone could give some
ideas?I am expecting your reply.


David,

You are the only one to get it correct so far. Martin and Vladimir both
failed. The question was how to _calculate_ the bits in a byte. Looking up
CHAR_BIT is not a calculation.
Rod Pemberton
Feb 21 '06 #5
Hi, Rod,

Thanks for your reply. Martin and Vladimir have given a solution to
find bit size of byte. Maybe Joe was not intended to get such anwser
from interviewee, it is still an efficient way :).

I wander whether there exists such case when we don't know the bit size
of byte in a machine but we need to calculate it in programming
language? It's just like kidding, since we don't know bytesize in the
machine, how could we know which programming lanugage would work on it?

Feb 21 '06 #6
Hi, Rod,

Martin and Vladimir has given a shorten method to find bit size of a
byte in c :). I think maybe Joe was not intended get an answer of
calculation, not looking up in an included file.

In programming language like C, compiler may set CHAR_BIT predefined.
But I wonder whether there is some cases when we don't know the bit
size of a byte in hardware CPU, and should calculate it by programming
language. The case's just like kidding, since we don't know bits of
CPU, how could programming language would work on it?

Feb 21 '06 #7

david ullua wrote:
Hi, Rod,

Thanks for your reply. Martin and Vladimir have given a solution to
find bit size of byte. Maybe Joe was not intended to get such anwser
from interviewee, it is still an efficient way :).

I wander whether there exists such case when we don't know the bit size
of byte in a machine but we need to calculate it in programming
language? It's just like kidding, since we don't know bytesize in the
machine, how could we know which programming lanugage would work on it?


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.c om, 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/googlegroupsrep ly/>

--
BR, Vladimir

Feb 21 '06 #8
david ullua wrote:
Hi, Rod,

Martin and Vladimir has given a shorten method to find bit size of a
byte in c :). I think maybe Joe was not intended get an answer of
calculation, not looking up in an included file.

In programming language like C, compiler may set CHAR_BIT predefined.
But I wonder whether there is some cases when we don't know the bit
size of a byte in hardware CPU, and should calculate it by programming
language. The case's just like kidding, since we don't know bits of
CPU, how could programming language would work on it?


To complicate matters, char is not required by the C standard to be the
machine "byte". It is only required to be at least 8 bits. And a
machine "byte" is not always 8 bits. And some ancient beasts even
enables the programmer to specify how many bits are in a byte. So on
those machines (I believe Unisys was one) you "defined" how many bits
was in a byte rather than test it.

Your code fails on a machine with 6 bit bytes and a C compiler with 12
bit chars (remember 6 bit bytes are not allowed by the standard).

So, given that it can't strictly be done in C the only "portable"
answer is to use an electron microscope to count the number of
flip-flops in a register used in byte oriented operations. Once you go
into that territory (if you have $$, there are several companies in
Taiwan willing to do it for you) then this problem is no longer
relevant to C and is therefore OT here.

Note that even if you do decide to go the electron microscope route
you'll still fail on machines which allows the programmer to define the
size of "byte" since it really is up to the programmer, not an inherent
property of the machine.

Feb 21 '06 #9
Rod Pemberton wrote:

You are the only one to get it correct so far. Martin and Vladimir both
failed. The question was how to _calculate_ the bits in a byte. Looking up
CHAR_BIT is not a calculation.


Sure it is. Grow up.
Feb 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.