C++ is saying that -1 < 13 (in programming) is false [duplicate] - c++

This question already has answers here:
Why does this if conditional with string.length() evaluate inconsistently?
(2 answers)
Signed/unsigned comparisons
(6 answers)
Weird std::string::size() in a for loop
(2 answers)
Closed 5 months ago.
I have a string called in, a string that has the value of Hello, world!. I also have a integer called i that has the value -1. When I ask C++ to print out if i is less than the length of in (in.length()), it says false but when I try -1 < 15, it says true. Why does it say false? I feel like this is extremely basic math?

string::length() returns an unsigned integer. You can't compare that to a negative signed value, so the -1 gets converted to an unsigned value, which wraps it to a very large number, which is not less than the string's length, hence the result is false.
-1 < 15, on the other hand, is comparing two signed integers, so no conversion is needed, and the result is true, as expected.

Related

use of "&" operator in conditional statement in C++ [duplicate]

This question already has answers here:
why does this work? (finding odd number in c++)
(9 answers)
Closed 2 years ago.
int main()
int n=5;
if(n & 1)
cout<<"odd";
else
cout<<"even";
how we are getting even or out using "&" operator
it should have been like this
if(n%2!=0) to check the even or odd.
can anyone explain what is that code doing.
What "n & 1" does here is that it returns the bitwise AND between n and 1. In other words, this is checking whether the last bit of n is equal to 1.
If it is, then n is odd, because the least significant bit of all binary representations of odd numbers is 1. If it's not, then n is even, because the least significant bit of all binary representations of even numbers is 0.

why (-1) % vector::size() always returns 0 [duplicate]

This question already has answers here:
Modular operation (%) provides false output
(3 answers)
Closed 2 years ago.
I wrote this code in C++.
vector<int> array = {1,2,3,4};
int i = (-1) % array.size();
Value i always has 0, but I want to get -1.
When I change the code into this with cast.
vector<int> array = {1,2,3,4};
int i = (-1) % (int)array.size();
Then i has -1.
I searched some attribute of vector::size() function in the internet.
However, I didn't understand.
Can anyone answer why this happens.
Thanks a lot.
Thanks for comment lgor.
This is surely because (-1) % array.size() converts int -1 to size_t. Thus becoming 0xFFFF... depending on the exact type of size_t. Taking it modulo 4 results in 3.
Not sure why you got 0. Probably in your code size of the array was 3 despite what you wrote - in which case result would be 0.
Edit: see the arithmetic conversion rules as to why the computation is done in size_t and not another type: https://en.cppreference.com/w/cpp/language/operator_arithmetic

strange behaviour with C++ bool data-type [duplicate]

This question already has answers here:
C++ : integer constant is too large for its type
(2 answers)
Closed 3 years ago.
This is a curiosity question. I was working with a Boolean to keep track of some parts of my code. I had the Boolean, say track initialised to be false. Now when I change it somewhere else to true using integer constant like :
track = 1;
this is defined. I understand how this would work true being 1 and false being 0. But now when you I say
track = 500;
this is still defined. Reasonable since it's any value other than or greater than 0 meaning it's true. My confusion now is when I do
track = 2147483648
which is 1 greater than INT_MAX the behaviour is still defined as true. Even when I push it a bit further to 2147483649454788. But when I equate to 21474836494547845784578 it throws an error
error: integer constant is too large for its type [-Werror]
_softExit = 21474836494547845784578;
^~~~~~~~~~~~~~~~~~~~~~~
Now this is just confusing. I'm pretty new to C++ so I'm not sure why or what any of this means. I know I could just use track = true; but I'm just curious.
As you have discovered yourself, an int object implicitly converts to a bool. So does a long long (or std::int64_t). So far so good, but the compiler message you show has nothing to do with bool. It's just that what it says: in your program, you have an integer literal that doesn't fit into into the domain that built-in integer types can handle. Hence the error, you would get it without trying to initialize a bool.
So this is ok:
const bool test = std::numeric_limits<long long>::max();
while inserting the actual literal value that std::numeric_limits<long long>::max() yields +1 is not ok.

Assigning out-of-range (negative) values to unsigned integral types [duplicate]

This question already has answers here:
What happens if I assign a negative value to an unsigned variable?
(7 answers)
Closed 6 years ago.
Okay, this maybe a dumb question. But, here it goes.
If i assign a negative value to an unsigned integral type in C++ like "unsigned short a = -1".
The value of a in the above example is set to be 65535 (2^16 - 1). And i know that if i set a value out of range to an unsigned integer, the value set will be the modulo of the number with the max size storable (65536 in this case), can you please explain the math being worked out behind the scenes?
How is (-1) modulo 65536 = 65535 ? Shouldn't it be -1 itself?
It is a difference of 1 MSB bit. In signed, that 1 bit is used to store the negativity of the number. While in unsigned, that is used to store the value. Its not math that works internally, its basically bit pattern manipulation that makes the difference between the two.

Why does "n&1 == 0" always return false? [duplicate]

This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Closed 6 years ago.
Why does the expression n&1 == 0 always return false, where n is an integer?
I want to use bitwise operation to determine whether n is even. However, it always return false. (The clion also prompted me that it always returns false).
What's more, it works when I use n&1 != 0 to determine whether n is odd.
Its because of the operator precedence.
== has higher precedence than the & operator, so 1 == 0 gets evaluated first to 0. Then the bit wise AND is performed which ultimately returns false.