+ 3
Let me try explaining more directly.
The way to make the highest value of an unsigned integer is to turn on all bits. E.g., in 8-bit integers - bits 0-7 - the value is 255 (11111111). In 16-bit integers - bits 0-15 - that is 65535. In general, the highest value is 2^[#bits] - 1.
What do you get if you turn on all bits of a signed integer? You get a negative number because the sign bit is on.
How do you interpret the negative value out of the remaining bits after the sign bit? Use two's complement. The two's complement is found if you flip the bits then add 1. E.g., for 8 bits, after the sign bit the remaining 7 bits are all 1 (1111111). Flip them (0000000). Then add 1 (0000001). You see, the signed interpretation of 11111111 (all bits on) is -1.
The easiest way to turn on all bits of an int, whether signed or unsigned, therefore, is to assign it a value of -1.
+ 3
When doing a string operation that might return npos, you should never have to know the value of npos. Just check whether the return value equals npos in order to know whether it was unsuccessful.
+ 3
When using a substring method that slices a length of string from a given point you also would specify how many characters to slice. If you simply want it to go until the end of the string, then you don't have to calculate precisely how long that substring will be. Just put std::npos for the length parameter. It will stop at the end of the string.
+ 2
A signed int uses the highest bit as a flag to indicate negative values. If you overflow a signed int, it wraps around from the highest positive value that it can hold to the lowest negative value it can hold.
E.g., in 8 bits:
127+1 = 01111111+1 = 10000000
= 128 unsigned
But if it is signed, then it overflows. The bit pattern is the same (10000000). Notice that the high bit is set, so it gets represented as negative. It wraps around to the lowest negative value:
= -128 signed
[Note that it is not negative 0! This can be understood by interpreting it within the two's complement representation.]
Now consider adding 127 more, so as to bring the unsigned value up to the highest value of 255 (all bits are turned on):
128+127 = 10000000+01111111
= 11111111 = 255 unsigned.
Do the same with the signed value:
-128+127 = -1 signed = 11111111.
Both are stored in the same bit pattern. They are merely represented differently on the output.
+ 1
Manav Roy
string::npos == numeric_limits<size_t>::max()
it is used as the return value by the string.find() if no match is found.
Other than that, I can't find other sample use for it.đ€
And I am not sure why we would need to do this if we could just get the length if the string and stop there. Maybe for really long strings where computing the length is costly so it is more practical to charge ahead and stop at the max limit.đ
. Seems extreme to me. Sololearn times out before that happens.
Perhaps someone could explain?
Maybe it is just mainly for string.find()
out of range return value error catching.
https://code.sololearn.com/ceSg2lrh1Du1/?ref=app
+ 1
Manav Roy
The concept of string::npos was a new one for me, so I went down that rabbit hole...
Interesting Stackoverflow discussion where one of the comments were from one of the original version designer of std::string.
https://stackoverflow.com/questions/35442718/why-do-we-have-stdstringnpos-but-no-stdvectornpos
+ 1
Manav Roy
I am amazed in your need to learn the tiniest detail in programming.
Here is something that explains sign bit in a more general way
https://www.electronics-tutorials.ws/binary/signed-binary-numbers.html
But really, I wonder if that much detailed knowledge is useful in more practical stuff. Maybe in an academic environment.
If learning is like walking in the forest, your way is like examining every leaf along the way.
Maybe you will gain more detailed knowledge, but you'll be missing the forest for the trees. And it is going to be a long walk. It's more fun to take a more casual walk and revisit it again and again to observe it in more detail.
Or maybe another way of putting it is that you can learn to drive a car or ride a bike with just a general knowledge of how it works. If you plan on being mechanic or manufacturer, then you learn the details. But not the other way around.
+ 1
Manav Roy
Get a good overview of the language first before getting lost in the details.
I'm worried you might get burned out from your attention to detail.
There is a lot to learn and it is impossible to master everything.đ
+ 1
Hi this is Palak
@Bob_Li you are correct, I am also expecting same answer, really very helpful.
Thanks
0
Nice