to calculate bitsize of a byte

D

david ullua

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.
 
M

Martin Ambuhl

david said:
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.
 
V

Vladimir S. Oka

david said:
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;
 
D

david ullua

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(dex)\n",i,c);
c = c<<1;
}while(c>0);
printf("bit count in a byte:%d",i);
 
R

Rod Pemberton

david ullua said:
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
 
D

david ullua

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?
 
D

david ullua

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?
 
V

Vladimir S. Oka

david said:
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.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/>
 
S

slebetman

david said:
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.
 
M

Martin Ambuhl

Rod said:
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.
 
A

Al Balmer

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.

In that case, the definition of "byte" has to be a part of the problem
specification.
 
A

Al Balmer

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?

Consider the possibility that you might want to calculate the size of
a character at run-time.
 
R

Richard G. Riley

Consider the possibility that you might want to calculate the size of
a character at run-time.

That is a lot easier than a BYTE isnt it?

Just left shift a bit x times on a defined "char" and detect it zeroing.
 
J

Jordan Abel

That is a lot easier than a BYTE isnt it?

Just left shift a bit x times on a defined "char" and detect it zeroing.

Except for overflow. And if this _does_ work, it will result in the same
answer as CHAR_BIT.
 
C

CBFalconer

Richard G. Riley said:
That is a lot easier than a BYTE isnt it?

Just left shift a bit x times on a defined "char" and detect it
zeroing.

You just created undefined or implementation defined behaviour.
You need to use an unsigned char, and need to store the shifted
value after each operation. Probably means a volatile unsigned
char. Of course the use of CHAR_BIT can't be allowed.

--
"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

Except for overflow. And if this _does_ work, it will result in the same
answer as CHAR_BIT.

I would hope so : I was more referring to the fact that a lot of
people wanted to muddy the waters with run time calculations of byte
size as opposed to compile time constants. "char" is easier because it
is a defined type so doing the bit shift really can and does work
without any worries of "purity" ...
 
R

Rod Pemberton

Martin Ambuhl said:
Sure it is. Grow up.

The OP also said it was an _interview_ question. If it came from Microsoft,
you'd be expected to _calculate_ it, not look it up. If you fail a simple
example like this on a job interview, your chances of getting the job
decrease. That's reality, whether you think it's immature or not.


Rod Pemberton
 
W

Walter Bright

david ullua said:
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(dex)\n",i,c);
c = c<<1;
}while(c>0);
printf("bit count in a byte:%d",i);

There's a bug. If char is signed, the c>0 will stop one bit short. Try c!=0.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers.
 
P

pete

CBFalconer said:
You just created undefined or implementation defined behaviour.
You need to use an unsigned char, and need to store the shifted
value after each operation. Probably means a volatile unsigned
char. Of course the use of CHAR_BIT can't be allowed.

Define an unsigned char with a value of -1,
and count the right shifts until zero.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top