clarification of int & int inside a if statement in C++ [duplicate] - c++

This question already has answers here:
Understanding the bitwise AND Operator
(4 answers)
Closed 8 years ago.
Does the statement return a single bit or concatenation of bits.
if(mask[i] & groupbit) {
//...
}
with:
i = an integer
mask[i] = an element of integer pointer
groupbit = An integer

It will result in an entire integer. When you use the bitwise and, each bit of the two values are and'ed together, and each bit in the result is set accordingly. The result will be the same number of bits as the values being and'ed together.
This is assuming you're using two integer variables.

Assuming mask is a pointer to an integer type the compiler will do the following:
Access the i-th element of the mask "array" as an integer
make sure both operands are of the same size (this may involve bit-expansion or truncation)
do a bit-wise AND
if there was a single bit set in the result from the previous AND operation the value will be treated as true, otherwise as false(if every single bit of the result is 0)

Related

Why can integer type int64_t not hold this legal value? [duplicate]

This question already has answers here:
Why does the most negative int value cause an error about ambiguous function overloads?
(3 answers)
Closed 3 years ago.
I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:
int64_t a = -9223372036854775808LL;
The error/warning is:
error: integer constant is so large that it is unsigned [-Werror]
I thought the number was out of range, so I tried:
std::cout << std::numeric_limits<int64_t>::min() << std::endl;
It outputs exactly the same number!!! So the constant is within the range.
How can I fix this error?
You may write
int64_t a = -1 - 9223372036854775807LL;
The problem is that the - is not part of the literal, it is unary minus. So the compiler first sees 9223372036854775808LL (out of range for signed int64_t) and then finds the negative of this.
By applying binary minus, we can use two literals which are each in range.
Ben's already explained the reason, here's two other possible solutions.
Try this
int64_t a = INT64_MIN;
or this
int64_t a = std::numeric_limits<int64_t>::min();

Bitwise NOT with boolean variable [duplicate]

This question already has answers here:
tilde operator returning -1, -2 instead of 0, 1 respectively
(2 answers)
Closed 6 years ago.
I'm a beginner at using bitwise operators and bool type. I might be wrong, but I thought bool type is represented on 1 bit and can take values from {0, 1}. So, I tried the NOT (~) operator with such a variable and the results are weird for me.
eg. for
bool x = 0;
cout << (~x);
I get -1. (I expected 1) Can you please tell me where I'm wrong and why only the ! operator does reverse the value (from 0 to 1 and from 1 to 0)?
Most processors do not have a 1 bit wide general purpose register so when you use a boolean it takes up whatever the default register size is on that platform (ie 64 bits on most Intel and ARM computers these days, but maybe 32 bit on some embedded systems). When you negate some thing that is all zeros, you get all 1's. In twos complement this evaluates to -1 in signed decimal. Long story short, your bool is really an int and ~0 is -1
The ! operator is a logical operator - hence 0 (false) is negated to 1 (true).
The ~ operator is a bitwise operator - hence every bit is negated. A bool, while notionally a single bit - can results in expressions of type int. Hence you are really negating 0.....000, which is 1...111, which is -1 (see two's complement).
The bool value x is implicitly converted to an int when used in the expression ~x. And the vast majority of computers use two's complement representation of signed integers, in which ~0 is equal to -1.
The ! operator is defined so that !x has type bool rather than int, so this issue doesn't occur.

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.

Is it possible to send decimal value(8 bit) in bool ? If Yes then How? [duplicate]

This question already has answers here:
Can a bool variable store more than 0x01?
(6 answers)
Closed 6 years ago.
bool is 8 bits long
As above post describe , bool is 8 bit long.
So is it possible to send value 2 in bool variable.
i.e.
0000 0010 -> 2
(decimal representation)
eg: bool x;
How to send this '2' in above bool variable 'x' ?
Thanks
Not in C++, no. A bool can hold true or false. There is no way to store 2 in a bool without first invoking undefined behaviour. Once you have invoked undefined behaviour, anything can happen. (Including what you expected except when demo'ing to important clients).
Also, a bool is not necessarily 8 bits long. It must be at least as large as a char (because sizeof(bool) must be at least 1), and the limits on the range of values which an unsigned char can hold means that it must be at least 8 bits. OTOH, there is nothing to stop an implementation using a bool which is larger than char, and there actually are implementations where char is 32 or 64 bits (DSP chips in the main).
bool is 8 bits long
Not necessarily true. All the standards say is that it has to be capable of holding true and false: its sizeof is implementation defined. You can deduce that it must be at least 1 since the type of sizeof must be an integral type and it cannot be zero else pointer arithmetic on an array of bools would break.
So don't attempt to send the value 2 - you're bound to render the behaviour of your program undefined.

Adding long numbers gives me minus result? [duplicate]

This question already has answers here:
How disastrous is integer overflow in C++?
(3 answers)
Closed 8 years ago.
when i try to add two long numbers it gives me minus result :
#include<iostream>
using namespace std;
int main ()
{
int a=1825228665;
int b=1452556585;
cout<<a+b;
return 0;
}
This gives me:
-1017182046
It's overflowing of the type. When you add two big number that the result can't be stored in chosen type it get overfloved. In most cases it will wraped the number, but it's not defined in the standard. So in some compilers the result is undefined.
For int and other numeric types when program can't store this big number in it we can see a overflow of it.
Lets say that int can store number from -10 to 10, when you do this:
int a = 10;
int b = a+1;
You will get -10 in b or some random value (it can be anything because the result is undefined)
That's because the results overflows. Since the first bit in numeric signed data types is used for the sign representation. The specific representation is called Two's complement (Wikipedia article here). Practically a 1 in this bit maps to a - while a 0 to +. The solution to this problem is using a larger data type like long. Larger it means that the memory used to store it is bigger so the range of values increases.