bit masking

Discussion in 'C Programming' started by Merrill & Michele, Oct 10, 2004.

  1. 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
     
    Merrill & Michele, Oct 10, 2004
    #1
    1. Advertising

  2. Merrill & Michele

    Artie Gold Guest

    Merrill & Michele wrote:
    > 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

    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."
     
    Artie Gold, Oct 10, 2004
    #2
    1. Advertising

  3. Merrill & Michele

    Mike Wahler Guest

    "Merrill & Michele" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 10, 2004
    #3
  4. "Mike Wahler" <> wrote in message news:<dT4ad.955$>...
    >
    > 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.

    --
    Peter
     
    Peter Nilsson, Oct 11, 2004
    #4
  5. Merrill & Michele

    Dan Pop Guest

    In <> (Peter Nilsson) writes:

    >"Mike Wahler" <> wrote in message news:<dT4ad.955$>...
    >>
    >> 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.


    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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 11, 2004
    #5
  6. Merrill & Michele

    Dan Pop Guest

    In <> "Merrill & Michele" <> writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 11, 2004
    #6

  7. > > MPJ wrote:
    > >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


    > 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
     
    Merrill & Michele, Oct 11, 2004
    #7
  8. Merrill & Michele

    Richard Bos Guest

    "Merrill & Michele" <> 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.


    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
     
    Richard Bos, Oct 12, 2004
    #8
  9. Merrill & Michele

    Mabden Guest

    "Richard Bos" <> wrote in message
    news:...
    > "Merrill & Michele" <> 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.

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

    --
    Mabden
     
    Mabden, Oct 12, 2004
    #9

  10. > > > > Dan 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.


    > > > MPJ wrote:
    > > > 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.


    > > Richard wrote:
    > > 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.


    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?

    > > Secundis, whatever do you mean by "his laying of the implementation
    > > egg"? That phrase doesn't make sense to me.


    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
     
    Merrill & Michele, Oct 12, 2004
    #10
  11. Merrill & Michele

    red floyd Guest

    Merrill & Michele wrote:

    > 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.
     
    red floyd, Oct 12, 2004
    #11
  12. Merrill & Michele

    Richard Bos Guest

    "Merrill & Michele" <> wrote:

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

    > > Richard wrote:
    > > 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.

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

    > > Secundis, whatever do you mean by "his laying of the implementation
    > > egg"? That phrase doesn't make sense to me.

    >
    > We'll revisit that one.


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

    Richard
     
    Richard Bos, Oct 13, 2004
    #12

  13. > "Richard Bos" wrote:
    > [ 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.

    > > > Richard wrote:
    > > > 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.

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


    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.

    > > MPJ quoting K&R2:
    > > #include <stdio.h>
    > >
    > > main()
    > > {
    > > printf("hello, world\n");
    > > }
    > >
    > > Never mind style. Is it conforming?


    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.

    > > > Secundis, whatever do you mean by "his laying of the implementation
    > > > egg"? That phrase doesn't make sense to me.

    > >
    > > We'll revisit that one.

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


    Agreed. Since you seem not to know what the idiom means. MPJ
     
    Merrill & Michele, Oct 13, 2004
    #13
  14. Merrill & Michele

    Dan Pop Guest

    In <> "Merrill & Michele" <> writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 13, 2004
    #14
  15. Merrill & Michele

    Flash Gordon Guest

    On Wed, 13 Oct 2004 08:29:29 -0500
    "Merrill & Michele" <> wrote:

    <snip>

    > 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.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Oct 13, 2004
    #15
  16. > > Dan wrote:
    > >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?


    My compiler rejects nothing. That is ultimately why I'm going to reject my
    compiler. MPJ
     
    Merrill & Michele, Oct 13, 2004
    #16
  17. Merrill & Michele

    Dan Pop Guest

    In <> "Merrill & Michele" <> writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 13, 2004
    #17

  18. > > MPJ wrote:
    > > 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.


    > 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
     
    Merrill & Michele, Oct 13, 2004
    #18
  19. Merrill & Michele

    Dan Pop Guest

    In <> "Merrill & Michele" <> writes:

    >> > Dan wrote:
    >> >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?

    >
    >My compiler rejects nothing.


    Remove the semicolon from the program. Is your compiler still happily
    accepting it?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 13, 2004
    #19
  20. Merrill & Michele

    Flash Gordon Guest

    On Wed, 13 Oct 2004 11:59:36 -0500
    "Merrill & Michele" <> wrote:

    >
    > > > MPJ wrote:
    > > > 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.

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


    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.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Oct 13, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hai Nguyen

    Masking a textbox as # box

    Hai Nguyen, Jan 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Dinesh Verma
    Jan 12, 2004
  2. Replies:
    7
    Views:
    7,945
  3. Replies:
    3
    Views:
    1,765
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    981
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    179
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page