wanted to know about ~tilde operator? [duplicate] - c++

This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
Closed 6 years ago.
I have seen many sites and referred books and reach to a point that tilde(~) operator is used to do ones' complement but when i ran the following code snippet ,i got amazed by its output.Can anybody explain me the output ??
The output coming is -11 for the following code.
Any help will be appreciated.
#include <iostream>
using namespace std;
int main() {
int x=10;
cout<<~x;
return 0;
}

The tilde is a bitwise NOT operator, so what it does is invert bits. Since the int is signed, it uses 2's complement for negative numbers:
00001010 = 10
11110101 = -11

The operator concerned here is the http://en.cppreference.com/w/cpp/language/operator_arithmetic Bitwise not operator, which is reversing all the bits of the number 10, to get the number -11, which is resulted from reversing the bits.

Related

Why is unsigned left bit shift overflow different between runtime and compile time? [duplicate]

This question already has answers here:
Why doesn't left bit-shift, "<<", for 32-bit integers work as expected when used more than 32 times?
(10 answers)
Unexpected C/C++ bitwise shift operators outcome
(6 answers)
Why the output of `printf("%llu\n", 1ull << n);` and `printf("%llu\n", 1ull << 64);` is different in C++? (n=64) [duplicate]
(3 answers)
Is right-shifting an unsigned integer by its total number of bits UB ? [duplicate]
(1 answer)
Closed 23 days ago.
I have the following code in C++
#include <iostream>
using namespace std;
int main(){
unsigned long long a;
cin>>a;
cout<< (1ull<<64ull) << ' ' << (1ull<<a) << endl;
}
now inputting 64, the output is
0 1
In short, the compiler seems to use a circular shift at runtime. But from my reading of the relevant cppreference page, I think it's supposed to be normal modular arithmetic, so the bit should just disappear as in the compile-time version. I tested this on GCC 11, GCC 12 and clang 14 so it's extra unlikely to be a compiler bug. So what am I missing here?
from the quoted cppreference page
In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.
since 64 is the number of bits in unsigned long long on my machine, this is undefined behavior

Issue with power function? [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!
In the expression 1 / 6, both numbers are integers. This means that this division will perform integer division, which results in 0. To do a double division, one number has to be a double: 1.0 / 6 for example.
Integer literals 1 and 6 have type int. Thus in the expression
1/6
there is used the integer arithmetic and the result is equal to 0.
Use at least one of the operands as a floating literal. For example
a = 1.0/6;
or
a = 1/6.0;
or
a = 1.0/6.0;

I'm trying to do an equation in C++ but it keeps outputting 1 [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 3 years ago.
As the title says, this code is meant to calculate the probability of 2 people having the same birthday in a group of 5 but it just outputs 1, I'm fairly new to C++ so any help would be appreciated.
#include <iostream>
using namespace std;
int main(){
float p;
p=1-(364/365)*(363/365)*(362/365)*(361/365);
cout<<p;
}
Put a .0 on each number, that way is treated as a double instead of an integer. Integer division (364/365) equals 0
p=1.0-(364.0/365.0)*(363.0/365.0)*(362.0/365.0)*(361.0/365.0);
This is because after calculation 364/365 the calculates answer is an integer which is 0.
To make it work change it like this.
p=1-(364/365.0)*(363/365.0)*(362/365.0)*(361/365.0);
You need to cast the integers to floats as / rounds to the largest integer below the result when both types are int:
p=1-(float(364)/float(365))*(float(363)/float(365))*(float(362)/float(365))*(float(361)/float(365));

Division in C++ [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!
In the expression 1 / 6, both numbers are integers. This means that this division will perform integer division, which results in 0. To do a double division, one number has to be a double: 1.0 / 6 for example.
Integer literals 1 and 6 have type int. Thus in the expression
1/6
there is used the integer arithmetic and the result is equal to 0.
Use at least one of the operands as a floating literal. For example
a = 1.0/6;
or
a = 1/6.0;
or
a = 1.0/6.0;

Bitwise operators : why is ~35=-36? [duplicate]

This question already has answers here:
two's complement
(3 answers)
Closed 8 years ago.
so here is my code :
The aNSWER is -36
is -36 written as sign bit notation ?
i cant understand the bitwise conversion giong on, in using Dev c++
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
printf("%d",~35);
getch();
}
Most significant bit determines if the number is negative or positive.