bit masking

M

Merrill & Michele

A recent thread discussed turning 4 given bytes in a particular order to a
long. While studying this, I encountered K&R §2.9, where the following
expression is said to mask off all but the first seven bits:

n=n & 0177;

This would have seemed right to me if 1+2+4+...+64 (seven terms) equaled
something other than 127. Ideas? MPJ
 
A

Artie Gold

Merrill said:
A recent thread discussed turning 4 given bytes in a particular order to a
long. While studying this, I encountered K&R §2.9, where the following
expression is said to mask off all but the first seven bits:

n=n & 0177;

This would have seemed right to me if 1+2+4+...+64 (seven terms) equaled
something other than 127. Ideas? MPJ
Numbers that start with `0' are in octal. As examples:

0177 == 127
026 == 22
011 == 9

and so on.

HTH,
--ag
 
M

Mike Wahler

Merrill & Michele said:
A recent thread discussed turning 4 given bytes in a particular order to a
long. While studying this, I encountered K&R §2.9, where the following
expression is said to mask off all but the first seven bits:

n=n & 0177;

This would have seemed right to me if 1+2+4+...+64 (seven terms) equaled
something other than 127. Ideas? MPJ

0177 (and any integer literal beginning with digit '0')
is in base 8. Personally, I prefer to use hex constants
("0x" prefix) for expressing bit patterns (I can more readily
mentally convert them to/from binary).

-Mike
 
P

Peter Nilsson

Mike Wahler said:
0177 (and any integer literal beginning with digit '0')
is in base 8.

Note though that pp-number tokens do not have a base. A token like
0188 is perfectly valid and could be used with macro operators.
 
D

Dan Pop

In said:
Note though that pp-number tokens do not have a base. A token like
0188 is perfectly valid and could be used with macro operators.

pp-numbers that aren't evaluated by the preprocessor as integers and
don't survive translation phase 4 as single tokens are not particularly
relevant to *any* discussion about integer literals.

Dan
 
D

Dan Pop

In said:
A recent thread discussed turning 4 given bytes in a particular order to a
long. While studying this, I encountered K&R §2.9, where the following
expression is said to mask off all but the first seven bits:

n=n & 0177;

This would have seemed right to me if 1+2+4+...+64 (seven terms) equaled
something other than 127. Ideas? MPJ

Before using K&R2 as a reference text, you MUST use it as a tutorial, i.e.
read *all* of it (at least until chapter 8) in strict sequence.

Dan
 
M

Merrill & Michele

Dan Pop wrote:
Before using K&R2 as a reference text, you MUST use it as a tutorial, i.e.
read *all* of it (at least until chapter 8) in strict sequence.

Such was my resolve when I bought it sight unseen. I wanted my C
development to be ANSI compliant. Imagine my surprise when the first prog
had a non-conforming main() call followed by his laying of the
implementation egg. As per your advice, I'll continue sequentially. MPJ
 
R

Richard Bos

Merrill & Michele said:
Such was my resolve when I bought it sight unseen. I wanted my C
development to be ANSI compliant. Imagine my surprise when the first prog
had a non-conforming main() call followed by his laying of the
implementation egg.

Care to explain?
Imprimis, AFAIR, the first program from K&R 2 is quite conforming to the
Standard it uses. Remember that K&R was written before 1999, so it
necessarily uses C89, under which implicit int is legal.
Secundis, whatever do you mean by "his laying of the implementation
egg"? That phrase doesn't make sense to me.

Richard
 
M

Mabden

Richard Bos said:
Care to explain?
Imprimis, AFAIR, the first program from K&R 2 is quite conforming to the
Standard it uses. Remember that K&R was written before 1999, so it
necessarily uses C89, under which implicit int is legal.
Secundis, whatever do you mean by "his laying of the implementation
egg"? That phrase doesn't make sense to me.

Obviously, he refers to the phrase, "hello, world".
What happens when one tries to learn C in space???
 
M

Merrill & Michele

Is the following program ANSI C when I shelled out $40 for K&R2 in 2004:

#include <stdio.h>

main()
{
printf("hello, world\n");
}

Never mind style. Is it conforming?

We'll revisit that one.
Mabden wrote:
Obviously, he refers to the phrase, "hello, world".
What happens when one tries to learn C in space???

Sarcasm. MPJ
 
R

red floyd

Merrill said:
Is the following program ANSI C when I shelled out $40 for K&R2 in 2004:

#include <stdio.h>

main()
{
printf("hello, world\n");
}

Never mind style. Is it conforming?

Yes. Implicit int is legal in C89. K&R2 is written to C89, not C99.
 
R

Richard Bos

[ Please do not snip all references from a follow-up. Snip only those
references for which you also remove the quoted text. ]
Is the following program ANSI C when I shelled out $40 for K&R2 in 2004:

I don't give a damn how much you paid; the contents is the only thing I
care about.
#include <stdio.h>

main()
{
printf("hello, world\n");
}

Never mind style. Is it conforming?

And yes, this was conforming, as you'd have immediately realised if
you'd completely read my post you quote above. As I said, K&R was
written _before_ 1999. Therefore, it uses C89, and in C89 that code is
conforming. If you insist on a book that uses C99, I suggest you look at
its publication date before shelling out your much beloved forty
dineros.
We'll revisit that one.

I hope we won't, since it's still nonsense.

Richard
 
M

Merrill & Michele

:
[ Please do not snip all references from a follow-up. Snip only those
references for which you also remove the quoted text. ]

I'm still working on my snipping techniques. I am, at least, improving.
I don't give a damn how much you paid; the contents is the only thing I
care about.

When I buy a book in anno Domini 2004 with a huge ANSI stamp on the front of
it, I expect it to be relevant to the time on my receipt. I did not
purchase a historical tour.

The operative term in my query was the present tense use of the linking
verb: is.
And yes, this was conforming, as you'd have immediately realised if
you'd completely read my post you quote above. As I said, K&R was
written _before_ 1999. Therefore, it uses C89, and in C89 that code is
conforming. If you insist on a book that uses C99, I suggest you look at
its publication date before shelling out your much beloved forty
dineros.

The insinuation that I'm greedy will pass without comment.
I hope we won't, since it's still nonsense.

Agreed. Since you seem not to know what the idiom means. MPJ
 
D

Dan Pop

In said:
Is the following program ANSI C when I shelled out $40 for K&R2 in 2004:

#include <stdio.h>

main()
{
printf("hello, world\n");
}

Never mind style. Is it conforming?

It is a strictly conforming C89 program (if a program generating any kind
of output can be considered strictly conforming). Do you have a compiler
that rejects it when invoked in conforming mode?

Dan
 
F

Flash Gordon

On Wed, 13 Oct 2004 08:29:29 -0500

When I buy a book in anno Domini 2004 with a huge ANSI stamp on the
front of it, I expect it to be relevant to the time on my receipt. I
did not purchase a historical tour.

1) Complain to the publisher not us.
2) Think for a few seconds before buying a book. If it is the second
edition of the original book on the language then there is a fare chance
it does not cover the third version of the language.
3) Think about why it might say ANSI rather than ISO
4) Consider the fact that you are far more likely to use a compiler that
can properly support (modulo bugs) C89 and C99.
5) Read reviews of text books before you buy them or you might end up
with complete rubbish, although in this case IMHO you have one of the
(if not the) best C books.

Points 2 and 3 don't say for sure that the book does not cover C99, but
they do suggest that one should check more carefully if C99 is important
to you.
 
M

Merrill & Michele

Dan said:
It is a strictly conforming C89 program (if a program generating any kind
of output can be considered strictly conforming). Do you have a compiler
that rejects it when invoked in conforming mode?

My compiler rejects nothing. That is ultimately why I'm going to reject my
compiler. MPJ
 
D

Dan Pop

In said:
When I buy a book in anno Domini 2004 with a huge ANSI stamp on the front of
it, I expect it to be relevant to the time on my receipt. I did not
purchase a historical tour.

1. No point in making foolish assumptions. When buying a book, you look
at the publication date. This is the relevant year, not the one when
you bought it.

2. The *only* C standard ever produced by ANSI is C89. Therefore, the
book cover is telling the truth.

BTW, this is also the C standard in current use today and fully supported
by most compilers. I can easily imagine your anger/perplexity when
confronted with syntax errors generated by compiling examples from your
tutorial book, which can trivially happen if you use a C99 tutorial
book (there is only one such book, AFAIK, and not particularly good,
according to the ACCU review).

So, be happy that you didn't get what you actually expected!

Dan
 
M

Merrill & Michele

Flash Gordon wrote:
1) Complain to the publisher not us.
2) Think for a few seconds before buying a book. If it is the second
edition of the original book on the language then there is a fare chance
it does not cover the third version of the language.
3) Think about why it might say ANSI rather than ISO
4) Consider the fact that you are far more likely to use a compiler that
can properly support (modulo bugs) C89 and C99.
5) Read reviews of text books before you buy them or you might end up
with complete rubbish, although in this case IMHO you have one of the
(if not the) best C books.

Points 2 and 3 don't say for sure that the book does not cover C99, but
they do suggest that one should check more carefully if C99 is important
to you.
Thank you for your reply.
1) I wasn't complaining. I feel like I'm in Sunday school with the five
books of Moses containing two different sequences of the creation story, and
I can't get anyone to admit that one isn't right.
2) This text is a prereq for participation in this forum. I want to
particpate in this forum. Ergo, .... (Elapsed thinking time: 4
milliseconds)
3) At the risk of sounding stupid, I don't know the difference.
4) Please explain.
5) My library shows good judgement. MPJ
 
F

Flash Gordon

Thank you for your reply.
1) I wasn't complaining.

OK, it just read like a complaint to me.
I feel like I'm in Sunday school with the
five books of Moses containing two different sequences of the creation
story, and I can't get anyone to admit that one isn't right.

That is because both are correct ;-)

The original K&R C is also on topic here, although I would not recommend
using it myself.
2) This text is a prereq for participation in this forum. I want to
particpate in this forum. Ergo, .... (Elapsed thinking time: 4
milliseconds)

It isn't a prerequisite, but my opinion is that it was money well spent.
It is the only C book I have and I've never needed another for C
programming.
3) At the risk of sounding stupid, I don't know the difference.

The ANSI standard came out a year earlier, but there is no substantive
difference. As ISO is international and ANSI is just for the USA I would
expect books written after the ISO standard to refer to that (especially
in non-US editions) and not just the ANSI standard.
4) Please explain.

Two reasons.
a) Neither MS VC not gcc claim C99 conformance (GNU admit that their C99
mode is not yet fuly conforming).
b) I've only heard of 1 or 2 implementations claiming C99 conformance.

So if neither of the most common implementations for the desktop
implement it, and implementations are very rare, it is unlikely you will
use one in the near future.
5) My library shows good judgement. MPJ

Indeed.

You may also want to download for free, and what is more legally, a copy
of the last public draft of C99 to add to your library. You can get it
from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/ However, it is not an easy read.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top