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.
Related
This question already has answers here:
Why is initializing an integer in C++ to 010 different from initializing it to 10?
(4 answers)
Closed 1 year ago.
#include<bits/stdc++.h>
using namespace std;
int a[500];
int main()
{
a[21]=10;
if(a[21]==a[021])puts("Yes");
else puts("No");
return 0;
}
g++ -std=c++11
The output is No, can anyone tell me why?
In C++ a leading 0 on an integer literal means that value is in octal (similar to the way that 0x21 means that value is in hexadecimal).
Each of these values will be different. Here's a quick online demo:
http://cpp.sh/3cws4n
Note: the default output format for cout is decimal so the values you see are in decimal.
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));
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.
This question already has answers here:
How do I convert a value from host byte order to little endian?
(7 answers)
Closed 6 years ago.
I have a variable called length that is of type uint64_t. I want to convert it to 8 byte little endian long and I'm very confused about these data types.
Can someone guide me on how to make this conversion?
Well, there is Boost.Endian.
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <cstddef>
int main() {
std::uint64_t foo = 1;
std::cout << boost::endian::endian_reverse(foo);
}
Output: 72057594037927936
This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 8 years ago.
#include <iostream.h>
#include <math.h>
int main()
{
int j=2;
int output;
output=pow(10,j);
cout<<output;
return 0;
}
I wrote above code to gcc 12 compiler and got the output 99 instead 100. I don't get the valid reason while searching on various sites. Is there any compiler problem?
Because of integer truncation. pow() returns a floating point value, and due to floating point arithmetic, it is probably ~ 99.999...; however, due to integer truncation, even 99.999... gets truncated down to 99.