LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2019, 02:34 PM   #151
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Debian64 stable, LFS 7.1
Posts: 458

Rep: Reputation: 122Reputation: 122

2 to the power of 31 (2 to the 31st power). See Exponentiation (@jpollard touched upon it earlier).
 
2 members found this post helpful.
Old 01-16-2019, 09:51 PM   #152
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,547

Original Poster
Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Thanks again guys!

I've been reading the "Chapter 4 Variables, Data Types, and Arithmetic Expressions" and a lot of it does make enough sense now, but there are a few things I'm not too sure about. I done the exercises I could, but the last ones I'm not really too sure about.

When it says something like "1.7e4", which according to the C pdf is scientific notation; does the "e" mean "exponent" ?

BTW RT, the typo in your post #136, about floating point numbers, I fixed. But I kept forgetting to mention before that I saw that before you corrected it, and fixed it.

Code:
#include <stdio.h>

int main(void)
{
   int a = 1;
   float b = 1;
   int test_int_result;
   float test_float_result;

   test_int_result = a / 2;
   test_float_result = b / 2;

   printf("Test result of dividing an integer by 2: %d / 2 = %d\n", a, test_int_result);
   printf("Test result of dividing a float by 2: %.01f / 2 = %.01f\n", b, test_float_result);

return 0;
}

/* The error I got when I forgot to add the "#include <stdio.h>" header below:
 * 
 * [james@jamespc math-helpers]$ gcc -Wall -Werror floating_point_numbers.c -o floating_point_numbers 
floating_point_numbers.c: In function ‘main’:
floating_point_numbers.c:12:4: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
    printf("Test result of dividing an integer by 2: %d / 2 = %d\n", a, test_int_result);
    ^
floating_point_numbers.c:12:4: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror]
cc1: all warnings being treated as errors
*/
Chapter 4 Exercises

(I haven't included some of the details below, because the post is long enough as it is.)

Quote:
2. Which of the following are invalid variable names? Why?
6_05, _1312 - because you cannot use numbers (digits) in variable names.

_ - because there should be something after this.

Quote:
3. Which of the following are invalid constants? Why?
0X0G1, 0XABCDEFL - because hexadecimals only go from A to F.

0x10.5 - because the dot is not a valid hexadecimal character.

15,000 - because you cannot use commas in constants, it would have to written as 15000 without the comma.

Quote:
4. Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius
(C) using the following formula:

C = (F - 32) / 1.8
Code:
#include <stdio.h>

int main(void)
{
 
int F = 27;
float C = (F - 32) / 1.8;

printf("Fahrenheit (F) to degrees Celsius: %f\n", C);

return 0;
}
Quote:
5. What output would you expect from the following program?
It would display the character (char) "d" on the screen.

Quote:
6. Write a program to evaluate the polynomial shown here:

3 2
3x - 5x + 6

for x = 2.55.
I'm not sure what the "3x" with the little "3" above it, and the "5x" with the little "2" above it mean? Also, I have no idea what "for x = 2.55." means.

Quote:
7.Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 x 10-8 x 2.01 x 10-7) / (7.16 x 10-6 + 2.01 x 10-8)
Not sure what the little "-8" and "-7" mean? The "x" means multiply, doesn't it? I tried to read that link in post #151, but it doesn't make much sense to me, sorry.

Quote:
8.To round off an integer i to the next largest even multiple of another integer j,
the following formula can be used:

Next_multiple = i + j - i % j

For example, to round off 256 days to the next largest number of days evenly
divisible by a week, values of i = 256 and j = 7 can be substituted into the pre-
ceding formula as follows:

Next_multiple

= 256 + 7 - 256 % 7
= 256 + 7 - 4
= 259

Write a program to find the next largest even multiple for the following values of i and j:

i j
365 7
12,258 23
996 4
I'm really not sure about this one(s) ?

Like I said before, I tried to do as much as I could, but I really am not sure about the last couple of questions apart from what's obvious.
 
Old 01-17-2019, 06:49 AM   #153
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,504
Blog Entries: 13

Rep: Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007
1.) Most of your problems are understanding exponents. Research that topic more. The link offered by Andy Alt is excellent and 100% on the mark. If you have trouble with the topic, then web search for more resources that can explain it in ways which are easier for you to understand.

2.) I wouldn't get too hung up on recognizing good vs. bad variable names. The compiler will flag an error when you've used an illegal name.

Caviat:
- Interpreting a lengthy list of compiler warnings and errors can be an exercise in detective work.
- There are some really bad things a programmer can do, such as to use a C keyword as a variable.
- For instance, what if you named a variable "if", or "switch", or "float"?
- These are all special terms which mean something as part of control flow.
- You can't use them as variables, and the compiler will complain about them.
- But the error may not be exactly "You can't use 'switch' as a variable!"
- The error may say something like, "missing '(' identifier before 'switch'", or something like that.
- Eventually you'll realize that the use of 'switch' as a variable name is not allowed.
3.) % is the C modulus operator. Look it up, look up examples. And really. Because if you have difficulties with the 5-10 word description, that's about what I'd reiterate here, so that's not going to help. Read the description, and look at examples, then re-read the description, or other descriptions, and look at more examples. It's not rocket science, it is a rule applied to calculations. Once you learn it, you learn it. Until you do, you won't understand what code using that operator is doing.
 
3 members found this post helpful.
Old 01-17-2019, 11:46 PM   #154
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,547

Original Poster
Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
So does the little minus sign with the little number just mean it's a negative number instead of a positive number, but it's still multiplying that little number? Also, does the "x" still just mean multiply? As I'm still not 100% clear on it. I DID try reading other links.

Also, does the "C modulus operator" just mean it gives the remainder and that's it? I DID look at some examples, but that's the only thing I'm getting from it.

Is that all there is to it?

The following link is what I looked at as well as the link posted previously;

https://www.mathsisfun.com/algebra/n...exponents.html
 
Old 01-18-2019, 01:15 AM   #155
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,957
Blog Entries: 11

Rep: Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837
Quote:
Originally Posted by jsbjsb001 View Post
So does the little minus sign with the little number just mean it's a negative number instead of a positive number, but it's still multiplying that little number? Also, does the "x" still just mean multiply? As I'm still not 100% clear on it. I DID try reading other links.
If you by "little number" you mean the exponent with a leading minus sign, then no. Go back to the link you posted and look down the page to Negative Exponents, what does it tell you? That page looks very clear to me (in fact the site looks very well done).

When communicating to others about exponents in a format like this forum that does not support superscript fonts (the smaller raised font usually used for exponents), learn to use the the common alternatives:

Code:
8^2
8e2
8E2
All of which mean an 8 with an exponent of 2, meaning 8 squared or 8 to the second power. There is no meaning in 'e' vs 'E', personal preference.

For negative exponents simply prefix the exponent with a minus sign...

Code:
8^-2
8e-2
8E-2
Bonus point question: Why is 8^2 called "eight squared"?

Quote:
Originally Posted by jsbjsb001 View Post
Also, does the "C modulus operator" just mean it gives the remainder and that's it? I DID look at some examples, but that's the only thing I'm getting from it.

Is that all there is to it?
Well, yes and no...

Yes, the modulo operator shows the remainder of one number divided by another - that is exactly what it does...

Code:
8 % 5 = 3
Is that "all there is to it"? Yes, but that is a lot when you come to an understanding of how that can be useful!

So, I would say no, that is not "all there is to it" as in, "That is so simple I can forget about it".

Each little bit stands in relation to the whole in very important ways. Math is a structure of meaning, represented by a notation. You must learn both the notation and the meaning.

It is really just going to take time and effort. As rtmistler suggested, all that others can do is answer your questions with variations of the same simple words you find in books and in online places like the mathisfun site. Actually understanding what it means can only be done by you, there is no short-cut path...

Quote:
When Ptolemy I (ruler of Egypt from 323 BCE – 283 BCE) grew frustrated at the degree of effort required to master geometry via Euclid’s Elements, he asked Euclid whether there was some shorter path. The great mathematician is said to have replied “There is no royal road to geometry.” (found here)
Keep at it!

Last edited by astrogeek; 01-18-2019 at 01:16 AM.
 
1 members found this post helpful.
Old 01-18-2019, 05:27 AM   #156
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,547

Original Poster
Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Yeah, sorry, I didn't read about "Negative Exponents" properly. So it just means that instead of multiplying you're dividing instead, correct?

Thanks astrogeek for explaining about the "common alternatives" for writing "exponents", as it really was starting to get quite confusing.

Bonus point question answer: According to this, it means to multiply the same number by itself, correct? Beyond that, I've got no idea bloody idea.

I'll look up "notation" and "meaning" in relation to mathematics later on, as I'm all "mathed out" today. I'm sorry, but this maths is absolutely killing me. As I now understand why I had absolutely zero interest in it while I was at school. I think I'll just understand what things mean rather than knowing all the answers if that makes sense, as this is just getting more and more depressing. I think I'll also just stick to the "mathsisfun" site, as it's about the only site that seems to explain things in an understandable way to me.

PS: Maths is NOT fun, pigs you know what it is!
 
Old 01-18-2019, 05:42 AM   #157
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,783

Rep: Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397
Quote:
Originally Posted by jsbjsb001 View Post
Yeah, sorry, I didn't read about "Negative Exponents" properly. So it just means that instead of multiplying you're dividing instead, correct?

Thanks astrogeek for explaining about the "common alternatives" for writing "exponents", as it really was starting to get quite confusing.

Bonus point question answer: According to this, it means to multiply the same number by itself, correct? Beyond that, I've got no idea bloody idea.
That depends on the context. For integer exponents, yes. But there are also non-integer exponents.

https://en.wikipedia.org/wiki/Exponentiation

Quote:
I'll look up "notation" and "meaning" in relation to mathematics later on, as I'm all "mathed out" today. I'm sorry, but this maths is absolutely killing me. As I now understand why I had absolutely zero interest in it while I was at school. I think I'll just understand what things mean rather than knowing all the answers if that makes sense, as this is just getting more and more depressing. I think I'll also just stick to the "mathsisfun" site, as it's about the only site that seems to explain things in an understandable way to me.

PS: Maths is NOT fun, pigs you know what it is!
Programming is nothing but math. The programming language of choice is just a specific notation for math for a class of problems.
 
2 members found this post helpful.
Old 01-18-2019, 06:53 AM   #158
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,504
Blog Entries: 13

Rep: Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007Reputation: 3007
It is true one does not need to be a PhD in Mathematics in order to be able to program. I can attest that there are some points in Math which go too far beyond my tolerance level. But meanwhile I've also learned that the rules or laws of Math are already very well concluded. Therefore if I'm dealing with a calculation that is complex, and I have someone more invested in those details telling me the algorithm, then I can code it.

When writing C; one can't really escape needing to understand the intricacies of pointers, the arithmetic for those pointers, and the various notations for them. And things like operators. Operators can be logical or bitwise, and also there are operators which do certain functions, and they are used a lot. Such as the modulus operator, or the complement operator, ~.

So these are everyday things. If someone doesn't understand them, or generally what they do, then they're going to be behind the curve. One very common thing is to have a bit mask and use that to either set a value, clear a value, or test a value, and many times one uses |=, or ^=, or &= along with ~ . Understanding that you have 32-bits and that is eight F's, 0xffffffff is important. You have to understand what really happens when you divide that by 2, or 4, or 8, or what happens when you invert it or if you want to clear a certain bit in that term, and how you do it.

I don't know whether you really, truly need to know exponential numbers very well for this. Why? Because it was hammered into my head during Elementary School and Junior High. At some point, we all knew it well enough that they didn't bother re-teaching it to us. But we also could do Chemistry which uses a lot of that notation, so we continued to use it. You've said you're missing that.

Exponents are sort of a building block for understanding the different bases of numbers and understanding the value of variable and expressions when they are written in Hex, Binary, or Octal. I don't spend my day talking in exponential notation, but I can assure you that I know what 2^16 represents, or 2^32, because I need those number many times. Fact is I need 2^8 a far greater amount of time because I deal a ton with byte sized operations. Yes, 2^8 represents a byte of 8-bits and it's maximum value for unsigned is 255, it's maximum value for signed is -127 to +127.

I have to know that if I divide something by 4, it is the same as a bit shift of that variable two positions to the right. These manipulations I'm talking about are slight uses of altering a number, by way of changing the exponential value of it. Very slight, however one does need to know the basics in order to be able to manage the design of their code.

So I can't tell you where to draw that line, with the exception that things like the examples they're covering with you that use oddball numbers like 2.44E-7 are NOT what I generally would deal with unless I was performing scientific calculations all the time.

Instead what I need to understand is:

I have a bit mask, and bit #15 needs to be cleared.

Let's say the value is 0xFFF0FABC
Bit #15 is defined as 0x00008000
To clear that bit I'd do something like: result = 0xFFF0FABC & ~0x00008000
Giving me an actual result of: 0xFFF0EABC

If you can't understand that, then figure out how to understand that, because it's pretty huge in C programming.

And by the way, the "normal" way that would look is:
Code:
#define OPERATOR_BIT_15 0x00008000

    uint32_t value, result;

    value = input_argument; // also a uint32_t type variable of some unknown value

    // Clear OPERATOR_BIT_15
    result = value & ~(OPERATOR_BIT_15);

    return result;
That is a very typical world that I live in on an everyday basis with C. Yes, it has to do with understanding what 2 "to the <something>" means, or understanding it in Hex. It doesn't have to do exactly with understanding the "quantitative" answer. Such as I really have ZERO idea what 2^15 exactly is. Sure I can think about it for some period of time and then conclude the answer, and then promptly check it on a calculator. Bottom line is I don't care, or don't need to care, instead I need to know that it is "bit 15", AND I need to recognize that it is set versus not, when I break my code in the debugger and examine a variable. So I can go, .... "Well, THAT's not right!" Turn to my colleague and go, "WHAT'S WRONG WITH YOUR #$@%!! HARDWARE NOW?!?!?" <engineering argument ensues> It's all in fun, we live for it.
 
2 members found this post helpful.
Old 01-18-2019, 08:08 AM   #159
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,413

Rep: Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600
Quote:
Originally Posted by astrogeek View Post
When communicating to others about exponents in a format like this forum that does not support superscript fonts (the smaller raised font usually used for exponents),
The superscript font can be faked with Unicode: 8²

Quote:
Code:
8^2
8e2
8E2
All of which mean an 8 with an exponent of 2, meaning 8 squared or 8 to the second power. There is no meaning in 'e' vs 'E', personal preference.
I would not interpret those all the same:
Code:
8^2 = 8² = 64
8e2 = 8 * 10² = 800
8E2 = 8 * 10² = 800
 
2 members found this post helpful.
Old 01-18-2019, 02:36 PM   #160
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,957
Blog Entries: 11

Rep: Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837Reputation: 2837
Quote:
Originally Posted by ntubski View Post
The superscript font can be faked with Unicode: 8²
Thanks for the link


Quote:
Originally Posted by ntubski View Post
I would not interpret those all the same:
Code:
8^2 = 8² = 64
8e2 = 8 * 10² = 800
8E2 = 8 * 10² = 800
You are of course correct, the 'e' notation is usually interpreted as a power of ten multiplier, not as a power of the number. Also commonly called scientific notation.

Which again demonstrates the necessity to understand both the notation and the meaning, to be able to ask the questions and to understand the answers!
 
1 members found this post helpful.
Old 01-19-2019, 02:52 AM   #161
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,547

Original Poster
Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Thanks again guys!

Thank you for your post as well ntubski, as it forced me to re-look at the "mathsisfun" site about "exponents". While I will admit that I couldn't understand what you were saying about the way you interpret the symbols used for "exponents"; I did work out what you meant by what you were saying, in that for "8e2" (and 8E2), you meant 10 * 10 = 100, and then 8 * 100 which equals 800, correct?

Assuming I'm right in what I said above, I think I'll move on with the C book/pdf, and I was having a look at "for" loops, and while most of it seems to make sense; I had a bit of a play around with it, and I'm not sure I understand it 100%.

I got two different results depending on what I done with the code;

Here's the first code I done:

Code:
#include <stdio.h>

int numberToAdd = 10000, init_value;

int main(void)
{

for ( init_value = 1; init_value <= numberToAdd; init_value = init_value + 5 )
  numberToAdd = numberToAdd + init_value;       
  
printf("\n\nNumbers added: %i\n\n", init_value);

return 0;
}
Which gives me:

Code:
[james@jamespc devel]$ ./for_loop 

Numbers added: 146546
Here's the second code I done:

Code:
#include <stdio.h>

int numberToAdd = 10000, init_value;

int main(void)
{

for ( init_value = 1; init_value <= numberToAdd; init_value = init_value + 5 )
  //numberToAdd = numberToAdd + init_value;       
  
printf("\n\nNumbers added: %i\n\n", init_value);

return 0;
}
Which gives me:

Code:
...
Numbers added: 9986

Numbers added: 9991

Numbers added: 9996
Now clearly in the second example it still does the loop, but gives me the output for each time it's added "5" to the initial value with the line of code I commented out. But in the first example, it just gives me the end result, but still does the loop.

I'm not even really sure how to ask the question I'm trying to, but I'm just at a bit of a loss to fully understand what's exactly happening here apart of the obvious. I did try re-reading the C book/pdf, as well as playing around with the above examples, but I'm still not fully understanding why I need the line below the for statement line (numberToAdd = numberToAdd + init_value;) to give me just the one line output - if that makes any sense at all. I understand WHAT that lines does (I *think*), but I'm just not getting a clear picture of exactly what's going on here. Sorry if my question doesn't make any sense - I just don't know how else to ask it.
 
Old 01-19-2019, 05:41 AM   #162
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,783

Rep: Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397
It depends on what you are trying to do.
Code:
int main(void)
{

for ( init_value = 1; init_value <= numberToAdd; init_value = init_value + 5 )
  //numberToAdd = numberToAdd + init_value;       
  
printf("\n\nNumbers added: %i\n\n", init_value);

return 0;
}
Note the loop you have:
Code:
for ( init_value = 1; init_value <= numberToAdd; init_value = init_value + 5 )
This is specifically for 1 to numberToAdd (which is 10000). Note, the "init_value = init_value + 5" is only performed AFTER the body of the loop.

Because there is no terminating ";" (you commented out the line) , the BODY of the loop becomes "printf("\n\nNumbers added: %i\n\n", init_value);"

and that ";" at the end terminates the syntax of the for loop.

So you get some 10000 prints.
 
1 members found this post helpful.
Old 01-19-2019, 10:18 AM   #163
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,413

Rep: Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600
Quote:
Originally Posted by jsbjsb001 View Post
I did work out what you meant by what you were saying, in that for "8e2" (and 8E2), you meant 10 * 10 = 100, and then 8 * 100 which equals 800, correct?
Yup.
 
1 members found this post helpful.
Old 01-19-2019, 11:34 PM   #164
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: CentOS at the time of this writing, but some others over the years too...
Posts: 2,547

Original Poster
Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Thanks jpollard, I think I now understand what I wasn't before. Being; that extra line (numberToAdd = numberToAdd + init_value;) adds another "5" to the initial value (being "1"), I'm sure this is what I wasn't understanding. As I terminated the line for the loop itself, then run it, then un-terminated it, and uncommented the extra line, then ran it again, and I could see the difference. So thanks again, your pointer was just what I needed, thanks.

Thank you ntubski for confirming that, it's also just what I needed.

I've been having another look at octal and hexadecimals and I think I now know where I was going wrong; I wasn't understanding that I need to go back to zero, for octal once I reach "7", and for hexadecimals once I reach "16". So it's the same problem I was talking about before; I just could not put my brain into reverse, because of my subconscious, because it's just not used to it. So I think I'm really beginning to understand octal and hexadecimals a lot better now. I *think* I now finally completely understand astrogeek's post #142 now, it just wasn't making sense before with the "+1", because I wasn't understanding the "go back to zero" part, which caused the confusion for me.

It finally started to click having another look at this: https://www.mathsisfun.com/numbers/bases.html

Just another question while I'm here: Have I finally understood properly now? (octal and hexadecimals that is)

Thanks again guys!

PS: I wrote a "number line" program for hexadecimals too:

Code:
#include <stdio.h>

int main(void)
{
  
printf("\n\n                                        Hexadecimal Number Line\n\n\
                            Numbers along the top are DECIMAL numbers (base 10)\n\
                            Numbers along the bottom are HEXADECIMAL numbers\n\n\
         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 26 27 28 29 30 31 \n\
         |--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|-- \n\
         1  2  3  4  5  6  7  8  9  A  B  C  D  E  F  10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F \n\n");
return 0;
}

Last edited by jsbjsb001; 01-20-2019 at 09:26 AM. Reason: added hexadecimal "number line" code
 
Old 01-21-2019, 04:26 AM   #165
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,783

Rep: Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397Reputation: 1397
The only thing that might be added is the number "0". Base 10 goes 0 1 2 3 4 5 6 7 8 9; hence 10 values for a digit.
Octal (base 8) goes 0 1 2 3 4 5 6 7; and hex (base 16) go 0 1 2 3 4 5 6 7 8 9 A B C D E F.

Incidentally, this continues with binary: 0 1.

Conversion to a given base starts by dividing the number by the desired base. The remainder is the current digit on the right... the quotient is what is still to be converted. Dividing that again, and the remainder is again the digit (to be placed to the left of the previous digit.

if using base 16 (or any base larger than base 10) you have to look up the glyph that corresponds. Using base 16 the decimal number 28, becomes 38/16 => 1 remainder 11, and the eleventh symbol in base 16 is C. If you divide the quotient again becomes 1/16 => 0 with a remainder of 1; and the corresponding symbol in base 16 is 1.

-------

Where things can get fun is that base 2, base 8, and base 16 are very commonly used, the concept can also be done for really strange bases - like 50.

What gets fun is that this base has enough symbols for all of the latin alphabet (26 symbols) with a good bit left over...

If you then assign the symbols " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.0123456789" (yes the first symbol is a space), then suddenly, you have a compression - you get to store three symbols in a 16 bit number.

In the same way of converting bases, if the remainder is 0, then the corresponding symbol is a space; if the remainder is a 1 then then symbol is "A" ... and you get to represent text in a compressed form...

DEC used this to save space in filesystems for 12 bit, 16 bit, 18 bit, 32 bit and 36 bit computers (some using a slightly different set of symbols) to save space for file names (from around 1959 to the end of Digital Equipment Corporation).

The result of using radix 50 allows 8 characters per name, a period, and 3 more characters for an extension all uppercase. On the byte systems (the PDP 11, a 16 bit machine and up) this allowed a file name to be compared with a minimum of instructions - as a single 4 byte (32 bit) value could hold an entire file name. And that improved performance by reducing the time it takes to find files.

And the characteristics of that filesystem (the 8 character name, a period, and a three character extension) showed up almost everywhere else at the time. It is where Microsoft got the 8.3 form for FAT/VFAT filesystems.

Last edited by jpollard; 01-21-2019 at 04:32 AM.
 
2 members found this post helpful.
  


Reply

Tags
c programming, learning c


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration