Like Tree4Likes

Why the output of my program is "0" when I multiply large numbers like 256 and 8192 i

This is a discussion on Why the output of my program is "0" when I multiply large numbers like 256 and 8192 i within the C Programming forums, part of the General Programming Boards category; Originally Posted by CommonTater The problem with 256 * 8192 is that it's too big for a 16 bit integer ...

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by CommonTater View Post
    The problem with 256 * 8192 is that it's too big for a 16 bit integer and since it is powers of 2, the result left in the lower 16 bits is all 0s.

    Do yourself a favor...
    Code:
    #include <stdio.h>
    
    int main (void)
     { 
        printf("%d\n", sizeof (int));
        printf("%d\n", sizeof (long int));
        printf("%d\n", sizeof (long long int));
        return 0;
      }
    If your answers are 2 4 4 instead of 4 4 8 .... you need a new compiler.

    The biggest number you're going to put into 2 bytes is 65,535
    The biggest number for 4 bytes is 4,294,967,295
    The bittest number for 8 bytes is 18,446,744,073,709,551,614

    Can you see why a new compiler might be a good idea?
    You really should use %u for the result of sizeof().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    The objective is to get the series up to the 10th element. I already know what's the problem on this one. the element 17,179,869,184 is really impossible for 4-bit data type like unsigned long long int. All it needs is a new compiler and/or a new library that can process 8-bit data type, as unsigned long long int is still a 4-bit data type. Am I right?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nagasaki View Post
    The objective is to get the series up to the 10th element. I already know what's the problem on this one. the element 17,179,869,184 is really impossible for 4-bit data type like unsigned long long int. All it needs is a new compiler and/or a new library that can process 8-bit data type, as unsigned long long int is still a 4-bit data type. Am I right?
    Four bits is too small to store 16. Four bytes however will store something like: 18,000,000,000,000,000,000.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Has four bytes become more than 32 bits when I wasn't looking?

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    Has four bytes become more than 32 bits when I wasn't looking?
    Code:
    #ifndef tabstoplooking
    #define CHAR_BIT 16
    #endif
    I was looking up ULL. /facepalm


    Quzah.
    Last edited by quzah; 08-15-2011 at 06:57 PM.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the header file limits.h and/or values.h, you have the info on your compiler's max data - for every data type. Pulling some data from both of them, you have:

    Code:
    #define MAXDOUBLE	1.797693E+308
    #define ULONG_MAX	0xFFFFFFFFUL
    This is in Turbo C ver. 1.01.

    So it's clear that double has WAY more range than you need, if you want to use it, and unsigned long, doesn't have the range to handle it all.

    If you don't want to work with a double for some reason, and assuming that you need to stay with Turbo C because your teacher requires it (or your program will be tested on a Turbo C compiler), the other way is to use either an array[] and work with the digits one by one, like you were multiplying by hand.

    We just had a thread on this recently, here.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    This is in Turbo C ver. 1.01.
    1. Gathers pitchforks and torches.
    2. Set up stand along road to Adak's house.
    3. ???
    4. Profit!


    Quzah.
    Last edited by quzah; 08-15-2011 at 08:44 PM.
    Hope is the first step on the road to disappointment.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nagasaki View Post
    The objective is to get the series up to the 10th element. I already know what's the problem on this one. the element 17,179,869,184 is really impossible for 4-bit data type like unsigned long long int. All it needs is a new compiler and/or a new library that can process 8-bit data type, as unsigned long long int is still a 4-bit data type. Am I right?
    Pelles C.. short int = 2 bytes, int and long int = 4 bytes, long long int = 8 bytes.

    Stop messing around with antique 16 bit compilers. Get something up to date.

    This is just one of many reasons why Turbo C should be shoveled into a large pile and burned.
    Last edited by CommonTater; 08-15-2011 at 08:46 PM.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What good will it do him to get a better compiler if his program will be graded based on the teacher's compiler, which is Turbo C?

    He could code up a great program, but still get an F grade on it.

    Of course, he can use a better compiler - starting the day after his Turbo C teacher's class is over.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Four bits is too small to store 16. Four bytes however will store something like: 18,000,000,000,000,000,000.
    Quzah.
    If we're talking integers... 4 bytes tops out at 4,294,967,295 and 8 bytes will get you 18,446,744,073,709,551,614

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    What good will it do him to get a better compiler if his program will be graded based on the teacher's compiler, which is Turbo C?

    He could code up a great program, but still get an F grade on it.

    Of course, he can use a better compiler - starting the day after his Turbo C teacher's class is over.
    What he has here is a genuine chance to show his backward teacher just how totally outdated his courseware really is...

    Me? I'd write the program in Pelles C and drive the point home hard!
    "Turbo C can't do this... look how easily this does it.... Teach, it's time for you to get with the times!"

    Nothing changes until someone has the temerity to demand change... I feel so sorry for these guys their teachers are ruining their chances of a very good career...
    Last edited by CommonTater; 08-15-2011 at 08:58 PM.
    manasij7479 likes this.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you REALLY believe his teacher will want a student to show him how "backward" he is? 'Cause right off the bat, I'm thinking "Oh hell no!".

    My first day in class, we were told what compiler the programs we submitted would be graded on. We had a choice of Turbo C or Turbo C. If our programs wouldn't compile, or wouldn't produce accurate results, they were graded down - naturally.

    This was decades ago, but that is where I believe these students are today. They're told to use Turbo C, and their programs will be graded according to how they compile and run, on a Turbo C compiler - ONLY.

    Some of these students are running DOS for their OS. How will they run Pelles C, in that case?

    You know I'm a big fan of Pelles C, and they should move up to a better compiler as soon as they can - but not at the risk of failing their class.
    laserlight likes this.

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Do you REALLY believe his teacher will want a student to show him how "backward" he is? 'Cause right off the bat, I'm thinking "Oh hell no!".
    The whole point is to get the students --all of them-- motivated to demand modern tools and up to date courseware.

    Pelles C is FREE it would cost the university exactly NOTHING to make the move so there is no excuse for this... none whatsoever.

    My first day in class, we were told what compiler the programs we submitted would be graded on. We had a choice of Turbo C or Turbo C. If our programs wouldn't compile, or wouldn't produce accurate results, they were graded down - naturally.
    My first day in electronics training, I realized right away that I knew more about the wonders of moving electrons than my teacher did... Wound up working the year as his TA... and helping him update his curriculum... You know, things like ... "We do that with transistors now...." At the end of it, the Dean of the school shook my hand and thanked me.

    This was decades ago, but that is where I believe these students are today. They're told to use Turbo C, and their programs will be graded according to how they compile and run, on a Turbo C compiler - ONLY.
    But it's not really about the university... it's about bad teaching sabotaging what might otherwise be a brilliant career...

    Some of these students are running DOS for their OS. How will they run Pelles C, in that case?
    Yet another thing that desperately needs fixing.

    You know I'm a big fan of Pelles C, and they should move up to a better compiler as soon as they can - but not at the risk of failing their class.
    Which is worse... failing one class out of progressive defiance or failing your entire career out of resignation?

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,163
    Quote Originally Posted by CommonTater
    The whole point is to get the students --all of them-- motivated to demand modern tools and up to date courseware.
    Admirable sentiment, but whether a bottom-up approach actually works depends on the culture of the institution, and I have a feeling that it will not work in Indian universities, especially given what Salem's link cites. Sometimes a student really has no choice but to play along with eyes wide open until he/she graduates and has a say because the alma mater wants his/her money in donations. Of course, if the student or his/her parents has the money and/or influence then it could be a different story, but otherwise just because many students demand for something does not mean they will get it.

    Quote Originally Posted by CommonTater
    Pelles C is FREE it would cost the university exactly NOTHING to make the move so there is no excuse for this... none whatsoever.
    Not really. There are "menu costs" of changing course materials and updating the computers, but then this should be the norm for such courses anyway.

    Quote Originally Posted by CommonTater
    Which is worse... failing one class out of progressive defiance or failing your entire career out of resignation?
    If that one class is an early pre-requisite, then failing that one class could mean failing to obtain the degree.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Admirable sentiment, but whether a bottom-up approach actually works depends on the culture of the institution
    That whole situation over there massively sucks...

    As a one time technical trainer my heart really goes out to these guys. I *know* how much damage an out of date curriculum can do and worked very hard keeping mine up to date. It was an industrial course, but some of the guys I had to teach from scratch, so giving them the best information was a very high priority both for me and for the company itself. One of the other teachers just cruised along with older notions and once the techs got wind of how out of date he was, he found himself, soldering iron in hand, as a lead technician in a branch office. There was so much pressure we almost had to fire him.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiply arbitrarily large real numbers
    By Martin0027 in forum C Programming
    Replies: 7
    Last Post: 05-17-2011, 02:59 PM
  2. Replies: 2
    Last Post: 01-30-2008, 06:12 PM
  3. How to use "switch" to perform large numbers cases?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 07-09-2003, 11:51 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21