This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 5 years ago.
I recently came across the following when I was testing my code for various value of x.
I will try to illustrate only the issue.
#include <iostream>
int main()
{
int x = 01234;
std:: cout << x ;
return 0;
}
Output:
when x = 1234 , 1234
x = 01234 , 668
x = 001234 , 668
x = 240 , 240
x = 0240 , 160
x = 00240 , 160
For mostly any number starting with 0 I get a different value.
eg: x = 0562 gives 370 and so on.
I tried using various online C++ compilers but all give same output.
I tried to google the issue but couldn't find an appropriate answer.
Looks like you've been hit with an octal literal! Any number literal beginning with just 0 is interpreted in base 8.
01234 = 1 × 8^3 + 2 × 8^2 + 3 × 8^1 + 4 × 8^0
= 1 × 512 + 2 × 64 + 3 × 8 + 4 × 1
= 512 + 128 + 24 + 4
= 668
0240 = 2 × 8^2 + 4 × 8^1 + 0 × 8^0
= 2 × 64 + 4 × 8 + 0 × 1
= 128 + 32
= 160
The number 01234 is in octal (base 8) when you prepend a 0 you define the number as an octal. When you then print it in decimal you get it's decimal equivalent
Related
I used an online converter to convert 412 from decimal to base 4 (which is 12130), and then applied the r's complement formula to get its 4s complement (which is 21210). However, in 6bits, 21210 becomes 321210.
When I try to convert it to decimal by doing
3 x - 4^5 + 2 x 4^4 + 1 x 4^3 + 2 x 4^2 + 1 x 4^1,
I get a number in decimal that is way larger than 412.
You have your "conversion to decimal" wrong -- it should be:
-1 x 4^5 + 2 x 4^4 + 1 x 4^3 + 2 x 4^2 + 1 x 4^1
note the difference in the first term, dealing with the sign -- the '3' digit has a value of -1 in a 4s complement sign digit.
How can I generate in SAS and ID code with 5 digits(letters & Numbers)? Where the first 3 must be letters and last 2 must be numbers.
You can create a unique mapping of the integers from 0 to 26^3 * 10^2 - 1 to a string of the format AAA00. This wikipedia page introduces the concept of different numerical bases quite well.
Your map would look something like this
value = 100 * (X * 26^2 + Y * 26^1 + Z * 26^0) + a * 10^1 + b * 10^0
where X, Y & Z are integers between 0 and 25 (which can be represented as the letters of the alphabet), and a & b are integers between 0 and 9.
As an example:
47416 = 100 * (0 * 26^2 + 18 * 26^1 + 6 * 26^0) + 1 * 10^1 + 6 * 10^0
Using:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You get:
47416 -> [0] [18] [6] (1) (6)
A S G 1 6
So 47416 can be represented as ASG16.
To do this programatically you will need to step through your number splitting it into quotient and remainder through division by your bases (10 and 26), storing the remainder as part of your output and using the quotient for the next iteration.
you will probably want to use these functions:
mod() Modulo function to get the remainder from division
floor() Flooring function which returns the rounded down integer part of a real numer
A couple of similar (but slightly simpler) examples to get you started can be found here.
Have a go, and if you get stuck post a new question. You will probably get the best response from SO if you provide a detailed question, code showing your progress, a description of where and why you are stuck, any errors or warnings you are getting and some sample data.
I am trying to figure out how to solve this problem (Project Euler):
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of
the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Using BigInt is not an option, I am trying to figure out how to implement a solution using only c++.
I thought maybe splitting the big numbers to an array of about 7 digit's long or something like that and then deal with them but i still cant figure out how to do this either..
thanks in advance!
Try this
#include "iostream"
#include "vector"
int n,remainder,sum;
int main ()
{
std::vector <int> digits(5000);
std::cin>>n;
digits[0]=1;
digits[1]=1;
for (int k=2;k<n+1;k++) {
for (int i=1;i<=digits[0];i++) {
digits[i]=digits[i]*k+remainder;
remainder=0;
if (digits[i]>9) {
remainder=digits[i]/10;
digits[i]%=10;
if (i==digits[0])
digits[0]++;
}
}
}
for (int i=digits[0];i>=1;i--)
sum+=digits[i];
std::cout<<sum;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why is initializing an integer in VC++ to 010 different from initialising it to 10?
This got me very confused, and I hope one of you can answer my question. How come this code will produce the output "116"?
#include <iostream>
int main()
{
std::cout << 0164 << std::endl;
return 0;
}
The code has been compiled with MSVC++ 2010 and g++ under Mac OS X.
"cout" can print '0' alone and '164' alone, but as soon '0' is the first digit in the number the output changes.
Because the 0 in front makes the number be interpreted as octal.
0164 =
4 * 1 +
6 * 8 +
1 * 64
= 116
Or, via binary:
0164 =
0 1 6 4 =
000 001 110 100 =
1110100 =
116
The same goes for hexadecimal numbers, you write them as 0x1FA for example.
In C and its brethren, a number with 0 on the front is octal, not decimal.
Hence your number is 1 * 82 (1 * 64 = 64) plus 6 * 81 (6 * 8 = 48) plus 4 * 80 (4 * 1 = 4) which equates to 116.
See here for a large treatise on what hexadecimal and octal are in C.
Can someone explain to me what this does?
#define ROUNDUP(n,width) (((n) + (width) - 1) & ~unsigned((width) - 1))
Providing width is an even power of 2 (so 2,4,8,16,32 etc), it will return a number equal to or greater than n, which is a multiple of width, and which is the smallest value meeting that criteria.
So width = 16; 5->16, 7->16, 15->16, 16->16, 17->32, 18->32 etc.
EDIT I started out on providing an explanation of why this works as it does, as I sense that's really what the OP wants, but it turned into a rather convoluted story. If the OP is still confused, I'd suggest working through a few simple examples, say width = 16, n=15,16,17. Remember that & = bitwise AND, ~ = bitwise complement, and to use binary representation exclusively as you work through the examples.
It rounds n up to the next 'width' - but I think width needs to be a power of 2.
For example width == 8, n = 5:
(5 + 8 - 1) & ~(7)
= 12 & ~7
= 8
So 5 rounds to 8. Anything 1 - 8 rounds to 8. 9 to 16 rounds to 16. Etc. (0 rounds to 0)
It defines a macro called ROUNDUP which takes two parameters, n and width, and returns the value (n + width - 1) & ~unsigned(width - 1).
:)
Try this if you think you know what it does:
std::string s("WTF");
std::complex<double> c(-11,5);
ROUNDUP(s, c);
It won't work in C because of the unsigned. Here is what is does, as long as width is confined to powers of 2:
n width ROUNDUP(n,width)
----------------
0 4 0
1 4 4
2 4 4
3 4 4
4 4 4
5 4 8
6 4 8
7 4 8
8 4 8
9 4 12
10 4 12
11 4 12
12 4 12
13 4 16
14 4 16
15 4 16
16 4 16
17 4 20
18 4 20
19 4 20