DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: cant understand this simple program

  1. #1
    sysdeamon Guest

    cant understand this simple program


    i am back & have brought with me the Author's answer to the Exercise about
    Flags.
    Please just let me know the logic of the answer the Author has provided to
    this exercise.
    Thank you all.
    Here it is :
    Exercise:
    Imagine that in a program we want to set a 'file open mode' variable based
    on two attributes: the file type (text or binary), & the way in which we
    want to open the file (read,write or append).Using the bitwise operation
    (& and |) & a set of flags, devise a method to allow an integer variable
    to be set to any combination of the two attributs. write a program which
    sets a variable & then decodes it, printing out its settings.

    # include <iostream>

    using namespace std;

    const int text=0x01;
    const int binary=0x02;

    const int read=0x10;
    const int write=0x20;
    const int append=0x40;

    int main()
    {
    int mode=text|append;
    if (mode & text)
    cout<<"mode is (text,";
    else if (mode & binary)
    cout<<"mode is (binary,";

    if (mode & read)
    cout<<"read)\n";
    else if (mode & write)
    cout<<"write)\n";
    else if (mode & append)
    cout<<"append)\n";

    return 0;
    }


  2. #2
    jonnin Guest

    Re: cant understand this simple program


    Kinda cryptic =)

    hes doing it in bits. a int has some bits (usually 32 these days), each
    set to on or off.

    these are "masks" or bit patterns for on and off schemes.

    >
    >const int text=0x01;
    >const int binary=0x02;
    >
    >const int read=0x10;
    >const int write=0x20;
    >const int append=0x40;
    >
    >int main()
    >{


    for example, but i may miss a number,

    text = 0001
    binary = 0010
    read = 0100
    write = 1000

    etc


    mode = text | read;

    mode = 0101

    etc.

    An inefficient way to do the same is have numbers represent a combination
    (say, 0 = text and append, 1 = binary and append, 2 = text and read, ...)

    and compare with a switch/case. Hes doing the same thing, but using logic
    speeds up the compare process and has other benefits.





    mode = text or append. this sets the text bits and the append bits.

    > int mode=text|append


    if text, because the bits in mode anded with the bits in text = true or not
    > if (mode & text)
    > cout<<"mode is (text,";



    > else if (mode & binary) //else again, same deal
    > cout<<"mode is (binary,";
    >


    more of the same.
    > if (mode & read)
    > cout<<"read)\n";
    > else if (mode & write)
    > cout<<"write)\n";
    > else if (mode & append)
    > cout<<"append)\n";
    >
    > return 0;
    >}
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center