{
int const * p=5;
printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
2or equivalently:
128 64 32 16 8 4 2 1 : decimal weightsThus, the binary number 10101001 has a decimal equivalent of
1x1 + 1x8 + 1x32 + 1x128 = 169If you assign contiguous 1s starting from the right, the above diagram can be used as a kind of calculator. Let's say you have 00001111 binary bits. To get the decimal equivalent, you could do the calculations the hard way, that is:
1x1 + 1x2 + 1x4 + 1x8 = 15or you could note the following (taking our number):
128 64 32 16 8 4 2 1 :decimal weightsIf you have all ones starting at the right side, you can simply take the weight of the first 0 bit (16 in this case), subtract 1, and you have 15—the decimal equivalent—without having to use a calculator. Thus, if all the bits on the right are 1s, you can determine the decimal value by using the above diagram as a kind of calculator. Note that the bits go up in powers of 2, so the ninth bit has a decimal weight of 256. So if you have a byte with all ones, i.e., 11111111, then it has a decimal value of 255 (256 -1). 255 appears many times in IP addressing.
0 0 0 0 1 1 1 1 :binary number
| 128 64 32 16 8 4 2 1 | decimal |
| 1 0 0 0 0 0 0 0 | 128 |
| 1 1 0 0 0 0 0 0 | 192 |
| 1 1 1 0 0 0 0 0 | 224 |
| 1 1 1 1 0 0 0 0 | 240 |
| 1 1 1 1 1 0 0 0 | 248 |
| 1 1 1 1 1 1 0 0 | 252 |
| 1 1 1 1 1 1 1 0 | 254 |
| 1 1 1 1 1 1 1 1 | 255 |
| 128 64 32 16 8 4 2 1 | Binary |
| 128 192 224 240 248 252 254 255 | Decimal |
| High-Ordered Byte | |||
| Class | Binary | Decimal | Decimal |
| Starting | Starting | Ending | |
| Point | Point | Point | |
| A | 0 | 0 | 126 |
| 127 (loop-back) | |||
| B | 10 | 128 | 191 |
| C | 110 | 192 | 223 |
| D | 1110 | 224 | 239 |
| E | 11110 | 240 | 247 |
| First | Second | Result |
| Bit | Bit | |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
0*1 + 1*2 + 0*4 + 0*8 + 1*16 + 1*32 + 0*64 + 1*128 = 178while the second byte has a decimal value of
1*1 + 1*2 + 1*4 + 0*8 + 0*16 + 1*32 + 1*64 + 0*128 = 103.Now, AND the two bytes:
1 0 1 1 0 0 1 0 178 decimal, ANDed withAs a second example, let's AND 178 with 255.
0 1 1 0 0 1 1 1 103 decimal
--------------- gives
0 0 1 0 0 0 1 0 34 decimal
1 0 1 1 0 0 1 0 178 decimal, ANDed withWe know, then, that when you bit-wise AND any byte (number) with 255, you get the number dropping through, i.e., the result is merely the number again.
1 1 1 1 1 1 1 1 255 decimal
--------------- gives
1 0 1 1 0 0 1 0 178 decimal
| Class | Default | Meaning of | Sample | Sample |
| Net-Mask | IP (Host) | Host | Network | |
| Address | Address | Address | ||
| A | 255.0.0.0 | N.H.H.H | 10.0.1.23 | 10.0.0.0 |
| B | 255.255.0.0 | N.N.H.H | 146.87.12.250 | 146.87.0.0 |
| C | 255.255.255.0 | N.N.N.H | 200.150.189.31 | 200.150.189.0 |
| Binary | Decimal |
| Number | Equivalent |
| 00000000 | 0 |
| 00100000 | 32 |
| 01000000 | 64 |
| 01100000 | 96 |
| 10000000 | 128 |
| 10100000 | 160 |
| 11000000 | 192 |
| 11100000 | 224 |
| Last Byte | What | Why |
| Addresses | Happens | |
| to Them | ||
| 0 | invalid | first subnet address |
| 1-30 | valid | hosts on first subnet |
| 31 | invalid | broadcast address of first subnet |
| 32 | invalid | second subnet address |
| 33-62 | valid | hosts for second subnet |
| 63 | invalid | broadcast address of second subnet |
| 64 | invalid | third subnet address |
| 65-94 | valid | hosts for third subnet |
| 95 | invalid | broadcast address of third subnet |
| 96 | invalid | fourth subnet address |
| 97-126 | valid | hosts for fourth subnet |
| 127 | invalid | broadcast address of fourth subnet |
| 128 | invalid | fifth subnet address |
| 129-158 | valid | hosts for fifth subnet |
| 159 | invalid | broadcast address of fifth subnet |
| 160 | invalid | sixth subnet address |
| 161-190 | valid | hosts for sixth subnet |
| 191 | invalid | broadcast address of sixth subnet |
| 192 | invalid | seventh subnet address |
| 193-222 | valid | hosts for seventh subnet |
| 223 | invalid | broadcast address of seventh subnet |
| 224 | invalid | eighth subnet address |
| 225-254 | valid | hosts for eighth subnet |
| 255 | invalid | broadcast for eighth subnet |
#include
using namespace std;
#include "anaword.h"
static const int ALPH_SIZE = 26;
Anaword::Anaword(const string & word)
: myWord(word),
myCounts(ALPH_SIZE,0)
// postcondition: constructed
{
normalize();
}
Anaword::Anaword()
: myWord(""),
myCounts(ALPH_SIZE,0)
{
}
void Anaword::normalize()
// postcondition: myCounts represents the letter signture of myWord
{
}
string Anaword::toString() const
// postcondition: return "bagel" or "gable", regular form of string
{
return myWord;
}
ostream & operator << (ostream & out, const Anaword & a)
// postcondition: a printed t stream out, out returned
{
out << a.toString();
return out;
}
bool Anaword::equal(const Anaword & rhs) const
// postcondition: returns true if and only if *this == rhs
// canonical/normalized form of word used for comparisons
{
return false;
}
bool operator == (const Anaword & lhs, const Anaword & rhs)
// postcondition: returns true if and only if lhs == rhs
{
return lhs.equal(rhs);
}
bool operator != (const Anaword & lhs, const Anaword & rhs)
// postcondition: returns true if and only if lhs != rhs
{
return ! lhs.equal(rhs);
}
bool Anaword::less(const Anaword & rhs) const
// postcondition: returns true if and only if *this < rhs
// canonical/normalized form of word used for comparison
{
return false;
}
bool operator < (const Anaword & lhs,const Anaword & rhs)
// postcondition: returns true if and only if *this < rhs
{
return lhs.less(rhs);
}
bool operator <= (const Anaword & lhs,const Anaword & rhs)
// postcondition: returns true if and only if *this <= rhs
{
return ! rhs.less(lhs);
}
#ifndef _ANAWORD_H
#define _ANAWORD_H
#include
#include
using namespace std;
#include "tvector.h"
// Used for finding anagrams: words with the same letters
// but which are different words, e.g., "bagel" a "gable"
// author: Owen Astrachan
//
// an Anaword object prints as a regular string, but
// compares using a normalized (also called canonicalized) form
//
// Example: the Anaword version of the string "bagel"
// prints as bagle, but will be compared with
// other Anawords as a vector of counts
// of one 'a', one 'b', one 'e', one 'g', one 'l'
// Since the counts for "gable" are the same, "gable"
// and "bagel" will be equal when compared using operator ==
//
// basically an Anaword takes a string and converts it to a twenty-six
// digit number based on the counts of a's, b's, c's, ... z's so that
// aardvark is represented as:
//
// 3 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0
//
//
// operations:
//
// Anaword(const string & word) -- construct from a string
//
// bool equal(const Anaword & rhs) -- compare rhs for equality
// bool operator == (lhs, rhs) -- compare Anawords lhs == rhs
//
// bool less(const Anaword & rhs) -- compare rhs for inequality <
// bool operator < (lhs,rhs) -- compare Anawords lhs < rhs
// bool operator <= (lhs,rhs) -- compare Anawords lhs <= rhs
//
// string toString() -- returns uncanonicalized "bagel"
// ostream & << operator(ostream, -- print using <<
// Anaword)
class Anaword
{
public:
Anaword(const string & word); // construct from string
Anaword(); // default (for vector)
bool equal(const Anaword & rhs) const; // compare for ==
bool less(const Anaword & rhs) const; // compare for <
string toString() const; // return "bagel" or "gable"
private:
void normalize(); // helper function, sorts
string myWord; // regular string: "bagel"
tvectormyCounts; // canonicalized form
};
bool operator == (const Anaword & lhs, const Anaword & rhs);
bool operator != (const Anaword & lhs, const Anaword & rhs);
bool operator < (const Anaword & lhs, const Anaword & rhs);
bool operator <= (const Anaword & lhs, const Anaword & rhs);
ostream & operator << (ostream & out, const Anaword & a);
#endif
#includeDo not run the program as it is (without implementing) Anaword::equal and Anaword::less since the call to QuickSort will cause an infinite loop.
#include
#include
using namespace std;
#include "anaword.h"
#include "prompt.h"
#include "tvector.h"
#include "sortall.h"
void FindAnagrams(tvector& list)
// pre: list contains list.size() elements
// post: all anagrams in list printed, one set of anagrams per line
{
QuickSort(list,list.size());
}
int main(int argc, char * argv[])
{
tvectorlist;
ifstream input;
string filename,word;
// use command line argument if it exists, else prompt user
if (argc > 1)
{
filename = argv[1];
}
else
{
filename = PromptString("enter file name ");
}
input.open(filename.c_str());
if (input.fail())
{
cerr << "could not open " << filename << endl;
exit(1);
}
while (input >> word)
{
list.push_back(Anaword(word));
}
cout << endl << "read " << list.size() << " words" << endl;
FindAnagrams(list);
return 0;
}