Modulo operator gives different results with leading zeroes [duplicate] - c++

This question already has answers here:
How does C Handle Integer Literals with Leading Zeros, and What About atoi?
(8 answers)
Closed 8 years ago.
Why does:
int test() {
return 00101 % 10;
}
return 5, while:
int test() {
return 101 % 10;
}
returns 1? I can't think of an explanation.

Integer literals beginning with 0 like
00101
is actually an octal constant.

00101 is octal value which is 65 in decimal so it returns 5.

00101 is in octal which is equal to 65 in decimal, so that is why the modulus operator will always give us 5.
You can do octal to decimal converstion on this link http://www.rapidtables.com/convert/number/octal-to-decimal.htm

Related

Value of uint32_t variable [duplicate]

This question already has answers here:
How to work with Base 8 (Octal) numbers?
(3 answers)
Octal number literals: When? Why? Ever? [closed]
(13 answers)
Closed 3 months ago.
uint32_t number = 00000000000000000000000000001011;
std::cout << number;
Why is the number's value 521 here?
Number literals starting with a zero are interpreted as octal numbers (base 8).
1011 in base 8 is 521.

how to store 0 in MSB of int datatype in C++? [duplicate]

This question already has answers here:
Print leading zeros with C++ output operator?
(6 answers)
Closed 12 months ago.
#include<iostream>
using namespace std;
int main()
{
int x = 0101;
cout<<x;
return 0;
}
The output I am getting is 101 but I want 0101 instead. what to do??
First of all, you should get 65 as 0101 is parsed as octal 101 (64+1).
If you want to use binary literal, you can prepend 0b
#include<iostream>
using namespace std;
int main(){
int x = 0b0101;
cout<<x;
return 0;
}
https://godbolt.org/z/5Pxqehz7P
Integer values are written in
4 forms in C++:
Decimal with digits 0-9: 1590, 4581
Octal with digits 0-7 and starting with a leading zero: 043, 077
Hexadecimal with digits 0-9 and a-f, starting with a leading 0x or 0X: 0xc0d3, 0X170
Binary from C++ version 14 onwards with digits zero and one, a leading 0b or 0B: 0b1011, 0B0101
You number (a.k.a numeric literal) 0101 is in octal form (in standard C++ anyway) as it has a leading zero.
As #peru mentioned, you have to convert your number into one of the four supported numeric literal forms mentioned above.
If you have a C++14 compiler, you can do 0b0101 directly. Chances are you have to stick with the other three forms.
Octal and hexadecimal are easier to convert to and from binary. It's a good idea to choose them when working with binary. Five is still five in octal and hexadecimal, of course...
The cout stream prints integers in decimal format. So you can look at your compiler documentation to see if there is a binary output option. Alternatively, you can implement it yourself. For an 8 bit number:
void print_bin8(int num){
for(int pos = 7; pos > 0; pos--){
int bitmask = (1 << pos);
int bit = num & bitmask;
if(bit != 0){ cout << 1;}
else{ cout << 0;}
}
Basically mask each bit out with an AND and print it MSB to LSB.

Take a digit of a string as an integer in C++ [duplicate]

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 2 years ago.
I want to add the first digit of a string 111 to the integer x = 0 so that it equals
x = 0 + 1 = 1
The following code takes the character 1 instead of the integer 1:
int x = 0;
string str = "111";
x += str[1];
std::stoi did not work either:
x += std::stoi(str[1]);
The simple way to convert a digit to an integer is to substract '0' from it.
x += str[0] - '0';
This works because the encodings of the decimal digits are guaranteed to be continuous. So subtracting the lowest digit gives you the digit value.
Your other error is that the first character of a string is str[0] not str[1].

why when I enter zeroes on the left of an integer , c++ borland compiler changes the integer value? [duplicate]

This question already has answers here:
What does it mean when a numeric constant in C/C++ is prefixed with a 0?
(7 answers)
Closed 7 years ago.
This code, when I run it on C++ Borland compiler 5.02
#include<iostream.h>
void main()
{
int x;
cin>>x;
cout << x;
}
When enter 0010 to x, the output is 8? And these for some entries that have zeroes on its left, but not all?
In C++:
Octal numbers have a leading 0.
Hexadecimal numbers have a leading 0x.
Otherwise, you have a decimal number.
Therefore:
0x10 is a hexadecimal number representing 16.
010 is an octal number representing 8.
10 is a decimal number representing 10.

VS2008 C++: int32_t num = 0502; // num = 322 [duplicate]

This question already has answers here:
Is 0 a decimal literal or an octal literal?
(3 answers)
Closed 9 years ago.
I'm using Visual Studio 2008 Express. If I debug this program, the variable num holds the value 322, even when it's initialized to 0502. What am I missing here?
int main()
{
int32_t num = 0502;
return 0;
}
int32_t is defined in the portable version of pstdint.h Version 0.1.12 as
typedef signed long int32_t;
0502 is octal, since it has prefix 0. 502 in octal is 322 in decimal.
If you start a integer value with 0 it is considered as an octal number, similarly 0x is used for hex.