Trying to decide which programming language I want to learn

In summary: C++ is considered a lower-level language, meaning that it is closer to the hardware and gives you more control over how your program runs. However, C# is a high-level language, which means it is easier to learn and use and has a lot of built-in libraries that make coding faster and more efficient. Both languages have their advantages and disadvantages, and it ultimately depends on what type of project you are working on. For gaming, C++ is often preferred because of its speed and control, but C# has become increasingly popular in game development as well. It's worth trying both and seeing which one you prefer working with.
  • #141
yungman said:
I don't think I learn fun(int, int) type, what is this?
This is the declaration or prototype of a function with two int arguments, and that returns an int value. In a declaration, the names of the parameters are optional, but in the actual definition, the names are required.
 
  • Like
Likes yungman
Technology news on Phys.org
  • #142
Mark44 said:
This is the declaration or prototype of a function with two int arguments, and that returns an int value. In a declaration, the names of the parameters are optional, but in the actual definition, the names are required.
Ah, it's like math function f(x,y) = x^2 + y^2.
 
  • #143
Mark44 said:
There can be more than two purposes. In C++, but not C (unless it has changed in recent new standards), a function or operator can be overloaded. Here's an example showing the declarations (prototypes) of a function named fun with four overloads.
C++:
int fun(int, int);
int fun(unsigned, unsigned);
int fun(float, float);
int fun(double, double);
A possible motivation might be that each overload adds a pair of numbers of each type. This is a bit fakey, since the function's return type is int, and all overloads must share the same return type.
Now that I understand fun() is a function with two arguments. How can the program have 4 lines each defining fun() differently particularly fun ( int, int) and fun ( double, double). You can have fun (double, double) be integer, but not the other way around.
 
  • #144
Hi
For once, I have no question! I am hot on the trod on Chapter 5. I looked at one example and I don't like the way the book did, I changed it. I even create functions inside the program so the main() can call and it works. This is on prefix and postfix
C++:
//Difference between prefix and postfix
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PostfixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1--;
    cout << "Result of Postfix decrement num2=num1--:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}

int PrefixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = ++num1;
    cout << "Result of Pretfix increment num2=++num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int main()
{

    PostfixInc();
    PostfixDec();
    PrefixInc();
    return PrefixDec();

}

I know this is kindergarten stuff for you guys, but I am happy! I can't wait to get to the next topic on boolean stuffs etc. Then the next chapter is If-Then-Else...About time to move onto some real programming.:biggrin::biggrin:
 
  • Like
Likes FactChecker
  • #145
Here are a couple comments on your latest program.
1. It's not a good idea to do this: "using namespace std;" There's the potential of creating problems for yourself if you happen to choose identifiers that are also using the the std namespace.
A better choice is this, since you are using only two of the identifiers in that namespace:
using std::cout;
using std::endl;

2. None of your functions return anything useful, so it would be better to define them all as void functions, like this. The only thing I've done is to change the return type, and comment out the return statement. It is an error for a void function to return a value.
C++:
void PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    // return 0;   -- commented out, since a void function cannot return a value              
}

3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.
 
  • Like
Likes FactChecker
  • #146
Mark44 said:
Here are a couple comments on your latest program.
1. It's not a good idea to do this: "using namespace std;" There's the potential of creating problems for yourself if you happen to choose identifiers that are also using the the std namespace.
A better choice is this, since you are using only two of the identifiers in that namespace:
using std::cout;
using std::endl;

2. None of your functions return anything useful, so it would be better to define them all as void functions, like this. The only thing I've done is to change the return type, and comment out the return statement. It is an error for a void function to return a value.
C++:
void PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    // return 0;   -- commented out, since a void function cannot return a value          
}

3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.

I only use what I learn in the 4 chapters and a part of the 5th chapter. I have not learn "void" function.
Can you explain why it's not a good idea to use using namespace std? What is an identifier?

I use " return PrefixDec()" because it gave me the correct display. I never learn this, just saw an example in chapter 2 or 3. Then I just experiment. If I put return in the earlier statement, it will end without going to the function after that. If I don't put return, it gave me an error.

Remember, I do a lot of guessing here as I am only at the beginning of the book. I am just trying things out. The book is not good in the sense it gave out examples with stuffs that have not been covered yet, they even point out it would be in the later chapters. So I just experiment on my own to get the right outcome.

What is the right way to call a function?
 
Last edited:
  • #147
Good comments, but I would like to clarify this:
Mark44 said:
3. Call each of the functions like this: PrefixDec();
The fourth function you called like this: return PrefixDec(); -- this doesn't make any sense, because the returned value is not being stored in a variable or being displayed.
It should run fine, but the idea of returning a hard-coded 0 hidden in the PrefixDec function is questionable. The main() can return a zero to indicate a normal completion or a non-zero error code. What it returns should not be hidden that way in a lower-level function. Typically, if a lower level function can not complete normally, it can return an error indicator and the main can determine what it should do about that. Then main can decide what completion code to return to the operating system.
 
  • #148
FactChecker said:
Good comments, but I would like to clarify this:

It should run fine, but the idea of returning a hard-coded 0 hidden in the PrefixDec function is questionable. The main() can return a zero to indicate a normal completion or a non-zero error code. What it returns should not be hidden that way in a lower-level function. Typically, if a lower level function can not complete normally, it can return an error indicator and the main can determine what it should do about that. Then main can decide what completion code to return to the operating system.
I never even look the display in the debugger "5.2.exe (process 3372) exited with code 0" until you said this. I got the right answer on num1 and num2, I got it to display nicely the way I want it, that's as far as I went on checking.

What does that mean?
 
  • #149
yungman said:
I never even look the display in the debugger "5.2.exe (process 3372) exited with code 0" until you said this. I got the right answer on num1 and num2, I got it to display nicely the way I want it, that's as far as I went on checking.

What does that mean?
It just means that your main() told the operating system that it completed with a completion code 0. It means nothing except that your code always returns 0. If your code did any error checking, you can make main() return a non-zero value (zero usually indicates a successful completion). You decide what the main() returned value should be for different situations. It's up to you. You can run your program in ways that will take special actions if the program did not complete normally.
 
  • #150
I went back and look, I copied pasted something wrong in the original program, main on "return 0" in all the subfunctions. Now it makes a little more sense to me. Everything is the same EXCEPT I put "return 0" at the end of every subfunction and at the end of the main().
Code:
//Difference between prefix and postfix
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PostfixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1--;
    cout << "Result of Postfix decrement num2=num1--:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}

int PrefixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = ++num1;
    cout << "Result of Pretfix increment num2=++num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int PrefixDec()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = --num1;
    cout << "Result of Pretfix decrement num2=--num1:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int main()
{

    PostfixInc();
    PostfixDec();
    PrefixInc();
    PrefixDec();

    return 0;
}

This book has very few problem at the end. I am waiting for the other 2 books, hopefully they have more assignments I can write programs. For now, I can only play around like this.BTW, how come there's no color on my program this time? Just black characters only, but with all the indents.
 
Last edited:
  • #151
Suppose your main() had a line like:

C++:
int anErrorOccured = 0;
...
...
int PostfixIncReturnValue = PostfixInc();
if( PostfixIncReturnValue  != 0 ){
    cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue  << endl;
    ...
    ... (take appropriate corrective action) 
    ...
    anErrorOccured = 5;  // Error code 5 indicates that a problem occurred in PostfixInc
}
...
...
return anErrorOccured;
Then your function PostfixInc could return an error code and it would be checked by main() and appropriate actions could be taken.
 
  • #152
FactChecker said:
Suppose your main() had a line like:

C++:
int anErrorOccured = 0;
...
...
int PostfixIncReturnValue = PostfixInc();
if( PostfixIncReturnValue  != 0 ){
    cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue  << endl;
    ...
    ... (take appropriate corrective action)
    ...
    anErrorOccured = 5;  // Error code 5 indicates that a problem occurred in PostfixInc
}
...
...
return anErrorOccured;
Then your function PostfixInc could return an error code and it would be checked by main() and appropriate actions could be taken.
I don't follow you. I put in the code in main(), I got rid of all the "..." and comments, it did something strange, but no error. Notice on the last program, I put in return 0 in all the subfunctions?

Why is the last program I posted have no color?
 
  • #153
I play with what you suggested, it is strange. I deleted 3 of the subfunction and just use postfixInc() to simplify and make it shorter. If you run it, it will display the num1 and num2 TWICE.

I intentionally set the condition IF statement to "if (PostfixIncReturnValue = 0)". As you can see, the last line of postfixInc() is "return 0", but the program won't display the error line.

C++:
//Difference between prefix and postfix
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 0;
}
int main()
{
    PostfixInc();
    int anErrorOccured = 0;
    int PostfixIncReturnValue = PostfixInc();
    if (PostfixIncReturnValue = 0)    {
     cout << "ERROR: Could not PostfixInc. Error code = " << PostfixIncReturnValue << endl;
    };
    return 0;
}
 
  • #154
The "code" in my post #151 was only meant as pseudocode to give you the idea. It was to show how main() could detect an error indicator returned from PostfixInc, but you have nothing in PostfixInc that would send an error indicator. The reason that you get num1 and num2 displayed twice is that you are calling PostfixInc twice. This code shows one of the most common errors in C and C++ (I have made this mistake a million times.):
yungman said:
"if (PostfixIncReturnValue = 0)"
That does not test if the return value is 0. Rather it sets PostfixIncReturnValue to zero. It needs to be "if (PostfixIncReturnValue == 0)"
 
  • Like
Likes yungman
  • #155
yungman said:
Can you explain why it's not a good idea to use using namespace std?
I gave a reason for this in post #145.
yungman said:
What is an identifier?
A name for a variable, function, class, and so on. In your program, num1, num2, cout, PrefixInc, and main are identifiers.
yungman said:
I use " return PrefixDec()" because it gave me the correct display.
Your version of PrefixDec() would behave exactly the same if you called it in either of these ways:
PrefixDec();
return PrefixDec();
The last line of your function is return 0; . This causes the value 0 to be moved to the EAX register on your computer, but the calling function, main() is not set up to do anything with that value.
Since main() doesn't do anything with the returned value, there's no point in having the return statement in any of your four functions.
yungman said:
What is the right way to call a function?
It depends on whether the function is intended to perform an action or return a value. A function the just performs an action should be a void function. A function that is intended to return a value should be called so that the returned value is saved in a variable or printed or used in some calculation.
Here's an example of a void function.
C++:
void Print42()
{
    std::cout << 42 << std::endl;
}
Call this function like so: Print42();

Here's an example of a function that returns a value.
C++:
int HalfOf(int number)
{
    return number / 2;
}
This function can be called in any of these ways:
int answer = HalfOf(18); // answer will be set to 9
std::cout << HalfOf(answer); // displays 4, since 9 /2 == 4
int val = HalfOf(2) + HalfOf(4); // val will be set to 3

I don't know if you have seen any examples of functions with parameters yet in the book you're using, but I'm sure you'll see them in whatever chapter discusses functions.
yungman said:
BTW, how come there's no color on my program this time?
Because the BBCode tag you used was [code] instead of [code=cpp].
 
  • Like
Likes yungman
  • #156
I have a more pressing question regarding VS. I first ran into problem with one program I wrote, so I was to exprimenting and "save .cpp as" "Temp.cpp" in another location(another folder). But it SAVED in .cpp of my original program ( meaning it erased my original .cpp that I intend to save). Then if I change the .cpp of my original program, it change my Temp.cpp that is in another location. So I ended up erasing both!

This cause me a lot of problem. All the other simulation programs, schematic, pcb layout programs, they all distinct this and never erase other file like this. How can I avoid this. This is causing me a lot of grieve.

I am running into a lot of problems with VS this last two days. I really don't have a chance to experiment with your suggestions as this is what causing the erasing and I lost most of the stuffs.

I ended up having to copy the .cpp from what I posted here before, but then I got wrong results when I ran the program. For the life of me, I just don't get it. That was the program that was working beautifully. I closed the VS, I even restarted the computer, it is still wrong. I double checked the .cpp, everything is right.
 
Last edited:
  • #157
I encounter a strange result I cannot explain.
C++:
#include <iostream>
using namespace std;

int PostfixInc()
{
    int num1 = 101;
    cout << "num1 = " << num1 << endl;
    int num2 = num1++;
    cout << "Result of Postfix increment num2=num1++:  num1 = " << num1 << "     " << "num2 = " << num2 << endl;
    cout << "  " << endl;
    return 1;
}
int main()
{
    PostfixInc();

   int PostfixIncReturnValue = PostfixInc();
    if (PostfixIncReturnValue |= 0) {
        cout << " The return code = " << PostfixIncReturnValue << endl;
    };
    return 0;
}
When I run this it, it display this:
num1 = 101
Result of Postfix increment num2=num1++: num1 = 102 num2 = 101

num1 = 101
Result of Postfix increment num2=num1++: num1 = 102 num2 = 101

The return code = 1


Notice it repeats the same two lines 2 times instead once? If I take out the IF-THEN state, then it just display once like intended. Seems like the IF statement somehow run the PostfixInc(), so if I run the program, it goes through the PostfixInc() twice. Why?

ThanksBTW, I now understand the "void" instead "int" if I don't want anything to return from the subfunction. I understand I don't need to return anything from PostfixInc() etc. I am just experimenting the suggestions from you guys. Like I have not study the IF-THEN statement yet. I don't want to bore you guys too much on things I have not learn and waste your time trying to explain to me. I should just keep going and learn them later. they should all be there in the next two chapters.
 
  • #158
You have two calls to PostfixInc. One on line 15 and another on line 17. Line 17 is only used to set a variable for the IF condition of line 18. If you remove line 18, an optimizing compiler might realize that there is no need for the variable PostfixIncReturnValue and might be removing the line 17 call.
 
  • Like
Likes yungman
  • #159
yungman said:
I have a more pressing question regarding VS. I first ran into problem with one program I wrote, so I was to exprimenting and "save .cpp as" "Temp.cpp" in another location(another folder). But it SAVED in .cpp of my original program ( meaning it erased my original .cpp that I intend to save). Then if I change the .cpp of my original program, it change my Temp.cpp that is in another location. So I ended up erasing both!
In the dialog box for "Save Program.cpp As...", if you don't change the path, it will change the name of your source code file to whatever you typed in, and you will have two source code files -- one with the old name, and one with the new name. This is pretty much the behavior of any software. Use Windows Explorer, not Visual Studio, to look in the directories under the top level for your program. You may think that VS erased the files, but I doubt that it did this.

If you modified the path shown in the dialog box, VS will save a copy of Program.cpp with a new name at the new path you specified.
 
  • Like
Likes yungman
  • #160
C++:
   if (PostfixIncReturnValue |= 0) {
NOOOOOOOOOOOOOOOOOO! That should be:
C++:
   if (PostfixIncReturnValue != 0) {

I'm not going to go into detail here but if you don't believe me run this code:
C++:
#include <iostream>
using std::cout;

int main() {
  int test = 2;
  if (test |= 2) {
      cout << "Didn't expect that!\n";
  }
  if (test |= 1) {
      cout << "That seemed to work, but wait...\n";
  }
  cout << test;
}
 
Last edited:
  • Like
Likes FactChecker and yungman
  • #161
FactChecker said:
You have two calls to PostfixInc. One on line 15 and another on line 17. Line 17 is only used to set a variable for the IF condition of line 18. If you remove line 18, an optimizing compiler might realize that there is no need for the variable PostfixIncReturnValue and might be removing the line 17 call.
I guess the only way is to pull the cout << " " part out of the PostfixInc() and just return the information, then cout<<" " once in main() to display once while access the PostfixInc() twice.

But anyway, now I know.
 
  • #162
pbuk said:
C++:
   if (PostfixIncReturnValue |= 0) {
NOOOOOOOOOOOOOOOOOO! That should be:
C++:
   if (PostfixIncReturnValue != 0) {

I'm not going to go into detail here but if you don't believe me run this code:
C++:
#include <iostream>
using std::cout;

int main() {
  int test = 2;
  if (test |= 2) {
      cout << "Didn't expect that!\n";
  }
  if (test |= 1) {
      cout << "That seemed to work, but wait...\n";
  }
  cout << test;
}
Problem with old eyes, I thought that was |.
 
  • #163
yungman said:
C++:
    int num2 = num1++;
This is not a good idea. num1++ is ok, we know that it increments num1. But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first? I can't remember*, and I don't want my code to depend on me remembering it properly. It is never necessary to do this (just replace with separate increment and assignment statements), and it won't even produce more efficient code (any modern compiler will optimise to the same code whether in one statement or two).

* Note that whatever the answer is there is a prefix incrementer ++num1 that does the opposite - now erase this from your memory. Only ever use i++ (and i--) and use them 'stand alone'.
 
  • Like
Likes FactChecker
  • #164
pbuk said:
This is not a good idea. num1++ is ok, we know that it increments num1. But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first? I can't remember*, and I don't want my code to depend on me remembering it properly. It is never necessary to do this (just replace with separate increment and assignment statements), and it won't even produce more efficient code (any modern compiler will optimise to the same code whether in one statement or two).

* Note that whatever the answer is there is a prefix incrementer ++num1 that does the opposite - now erase this from your memory.
I am just following the book, I am not to the point to judge, just an exercise on prefix and profix.

I think I should just keep working out the examples. The book throws out too many things that it has not covered yet, all these likely to be resolved after I go through a few more chapters.
 
  • #165
pbuk said:
But does int num2 = num1++; increment num1 first and then assign the value to num2 or does it do the assignment first?
The assignment operator has very low precedence, only one higher than the lowest operator, the comma operator. The post- and prefix operators (in that order) are at the top of the precedence list.
 
  • #166
Mark44 said:
The assignment operator has very low precedence, only one higher than the lowest operator, the comma operator. The post- and prefix operators (in that order) are at the top of the precedence list.
I don't have a C or C++ compiler to test it, but I think that is wrong. I can only test this in Perl, which I think follows the C precedence.

Using this Perl code:
$n1=1;
$n2=1;
$x1= ++$n1;
$x2= $n2++;
print "x1=$x1 x2=$x2\n";
I get this printed::
x1=2 x2=1
The issue is not how high the precidence of '=' is, but rather what the precidence on the right side is as far as which value of the variable is used on the right side (before or after the increment).
 
  • #167
FactChecker said:
I don't have a C or C++ compiler to test it, but I think that is wrong.
Yes, you are right -- it has nothing to do with precedence. That was a momentary brain lapse on my part. This statement
num2 = num1++;​
sets num2 with the value of num1 before the increment happens. After the sequence point (the semicolon in this case), num1 is incremented. Assuming the values in your Perl code, num2 is still 1, and num1 is 2.
 
  • Like
Likes FactChecker
  • #168
Mark44 said:
Yes, you are right -- it has nothing to do with precedence. That was a momentary brain lapse on my part. This statement
num2 = num1++;​
sets num2 with the value of num1 before the increment happens. After the sequence point (the semicolon in this case), num1 is incremented. Assuming the values in your Perl code, num2 is still 1, and num1 is 2.
In any case, this proves the point that @pbuk made. -- This is treacherous, hard to keep straight, and is safest to use only by itself.
 
  • Like
Likes pbuk
  • #169
I have a simple question to verify in this program:
C++:
//Bitwise Operator to perform NOT, AND, OR, XOR on individual bits in an integer
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    unsigned short inputNum = 0;
    cout << "Enter a number (0 - 255): "; cin >> inputNum;
    bitset<8> inputBits(inputNum);
    cout << inputNum << " in binary is " << inputBits << endl;

    return 0;

}
The unsigned short inputNum is 2bytes or 16 bits. But the bitset<8> is set to 8 bits only. Is the bitset<8> just specify using the L.S.8bits only? I actually change to read bitset<16> and read the M.S.Byte to be all 0.

Does this mean you can use long int and only specify to look at the number of bits by using bitset<>?

Thanks
 
  • #170
Another really stupid question, I don't have the bitwise NOT symbol on my keyboard. That is the horizontal wiggle symbol. Now what?
 
  • #171
The tilde ( ~ ) should be just to the left of the exclamation point (if it's really not there, you can use Alt 126), but C++ commonly uses ! to mean NOT. The tilde can be used for bitwise NOT; however, the tilde is also used for a class destructor.
 
Last edited:
  • Like
Likes yungman
  • #172
sysprog said:
The tilde ( ~ ) should be just to the left of the exclamation point (if it's really not there, you can use Alt 126), but C++ commonly uses ! to mean NOT. The tilde can be used for bitwise NOT; however, the tilde is also used for a class destructor.
I not only old, I am blind too. You don't know how many times I scan through the keyboard! There's nothing good about getting old other than better than the alternative.
 
  • #173
I just ran into problem running this program and I cannot figure out why it gave me an error. Also, it only display the first line that divide the number by 2 and won't display the other 3.
C++:
#include <iostream>
using namespace std;

int main()
{
    int inputNum = 0;
    cout << "Enter a number 0 to 255 = "; cin >> inputNum;// read in a number between 0 to 255.
    int halfNum = inputNum >> 1;
    int quarterNum = inputNum >> 2;
    int doubleNum = inputNum << 1;
    int quadrupleNum = inputNum << 2;

    cout << " inputNum divided by 2 = " << halfNum << endl;
    cout << " inputNum divided by 4 = " << quarterNum << endl;
    cout << " inputNum X 2 = " << doubleNum << endl;
    cout << " inputNum X 4 = " << quadrupleNum << endl;

    return 0;
}
I removed all my own codes, this is EXACTLY the one in the book, still something wrong. The error message is on line 1. This is the error message from VS:

Listing 5.8 error message.jpg


I don't see anything wrong. Please help

Thanks
 
  • #174
yungman said:
I have a simple question to verify in this program:
C++:
//Bitwise Operator to perform NOT, AND, OR, XOR on individual bits in an integer
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    unsigned short inputNum = 0;
    cout << "Enter a number (0 - 255): "; cin >> inputNum;
    bitset<8> inputBits(inputNum);
    cout << inputNum << " in binary is " << inputBits << endl;

    return 0;

}
The unsigned short inputNum is 2bytes or 16 bits. But the bitset<8> is set to 8 bits only. Is the bitset<8> just specify using the L.S.8bits only?
Yes. Your bitset instance can hold only 8 bits, so if you use a type to initialize this instance, only the low 8 bits will be used. If you change the declaration of inputNum to unsigned int, the result will be the same; that is, only the least significant 8 bits are used.
yungman said:
I actually change to read bitset<16> and read the M.S.Byte to be all 0.
If your bitset template instance is 16 bits, and inputNum is still unsigned short, then all 16 bits will be used in the initailization.
yungman said:
Does this mean you can use long int and only specify to look at the number of bits by using bitset<>?
Yes. Whichever bitset template type you use, the initialization will use the lower bits, assuming that your bitset template is for fewer bits than the type you use to initialize the template object.
 
  • Like
Likes yungman
  • #175
This is a link error, so this version of your code can not be run, but your description sounds like it started to run something old that was already linked.
 
Back
Top