Program to toggle nth bit in C

K

Keith Thompson

Frederick Gotham said:
Old Wolf posted:



You're right. Got to get out of my C++ habit of using char all the time
rather than unsigned char. Give me a slap if I do it again!

<OT>
Why is that a C++ habit? C++ has the same rules for plain char as C
does.
</OT>
 
M

Mark McIntyre

Walter Roberson posted:


We're dealing with computers, so 0.

Apparently you don't use Fortran, Pascal or Basic... :)

Also is the first bit the LSB or MSB?

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
A

Andrew Poelstra

Apparently you don't use Fortran, Pascal or Basic... :)

Also is the first bit the LSB or MSB?

The first bit is the rightmost. How this is used by the system
doesn't really matter, but for simplicity's sake, bits will be
labelled as such (in hexadecimal): FEDCBA98 76543210

The reason for this is that starting from anywhere other than
the right would require knowing the size of the type beforehand,
which complicates macros, however slightly.
 
B

Barry Schwarz

The first bit is the rightmost. How this is used by the system
doesn't really matter, but for simplicity's sake, bits will be
labelled as such (in hexadecimal): FEDCBA98 76543210

The reason for this is that starting from anywhere other than
the right would require knowing the size of the type beforehand,
which complicates macros, however slightly.

The limitations of your experience are showing. The largest publisher
of computing documentation labels the left most bit as bit 0.


Remove del for email
 
A

Andrew Poelstra

The limitations of your experience are showing. The largest publisher
of computing documentation labels the left most bit as bit 0.

I know that; I did spent a year learning Intel assembly language.
However, as far as quick C programs go, it's easiest to do the
reverse. That way, bit 0 of a char will be the same as bit 0 of
an int. (I'm assuming that sizeof (char) != sizeof(int) here).
 
W

Walter Roberson

I know that; I did spent a year learning Intel assembly language.
However, as far as quick C programs go, it's easiest to do the
reverse.

Andrew, you admit that are multiple valid definitions of which bit is
numbered 0, so why are you attempting to provide a definitive answer as
to what bit numbering system the original poster intended? Leave it
to the original poster to clarify exactly what the original
poster is trying to ask.
 
B

Barry Schwarz

I know that; I did spent a year learning Intel assembly language.
However, as far as quick C programs go, it's easiest to do the
reverse. That way, bit 0 of a char will be the same as bit 0 of
an int. (I'm assuming that sizeof (char) != sizeof(int) here).

It is not Intel, though the first letter is correct.


Remove del for email
 
M

Morris Dovey

Andrew Poelstra (in (e-mail address removed))
said:

||
|| The limitations of your experience are showing. The largest
|| publisher of computing documentation labels the left most bit as
|| bit 0.
|
| I know that; I did spent a year learning Intel assembly language.
| However, as far as quick C programs go, it's easiest to do the
| reverse. That way, bit 0 of a char will be the same as bit 0 of
| an int. (I'm assuming that sizeof (char) != sizeof(int) here).

I think Barry is referring to the NIH (Not Invented Here) maniacs who
seemed to think that calling the LSB "bit 0" was a bit too obvious -
so they chose a reversed bit numbering scheme that disassociated the
bit number from the power of two it represented.

On the other hand, they did gift the world with EBCDIC and a whole
assortment of other artful encoding creations. :)
 
F

Frederick Gotham

Keith Thompson posted:

<OT>
Why is that a C++ habit? C++ has the same rules for plain char as C
does.
</OT>


In C++, signed char is guaranteed to have no "invalid values" (just like
the situation is for unsigned char in C). The following is perfectly
legal in C++, but non-portable in C:


int main(void)
{
unsigned i = 58; /* Or any arbitrary value */


char *p = (char*)&i;

const char * const p_over = (const char*)(&i + 1);


do
{
*p++ += 3;
} while ( p != p_over );
}


Therefore, in C++, it's perfectly safe to access any object as if it were
an array of char's.

When writing C code, I have to make sure that I access the bytes as
unsigned char's, rather than just plain char.
 
C

CBFalconer

Morris said:
.... snip ...

I think Barry is referring to the NIH (Not Invented Here) maniacs
who seemed to think that calling the LSB "bit 0" was a bit too
obvious - so they chose a reversed bit numbering scheme that
disassociated the bit number from the power of two it represented.

On the other hand, they did gift the world with EBCDIC and a whole
assortment of other artful encoding creations. :)

OTOH on a machine with the more significant bytes in the lower
addresses (which is either big or little endian :) this convention
eases generic addressing of individual bits. When bytes are also
sized to a power of two, the bit/byte addresses can be extracted
with simple masks.
 
M

Mark McIntyre

The first bit is the rightmost.

Did you patent your telepathy machine? :)

(only the OP knows what he meant by the 'first' bit).
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top