+ Reply to Thread
Results 1 to 5 of 5

Thread: C language - How to write a C program to find the power of 2 in a normal way and in single step?

  1. #1
    Moderator GEEK's Avatar
    Join Date
    Nov 2006
    Age
    29
    Posts
    225
    Rep Power
    10

    C language - How to write a C program to find the power of 2 in a normal way and in single step?

    How to write a C program to find the power of 2 in a normal way and in single step?


  2. #2
    Junior Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    6

    Re: C language - How to write a C program to find the power of 2 in a normal way and in single step?

    Quote Originally Posted by GEEK View Post
    How to write a C program to find the power of 2 in a normal way and in single step?
    if((n&(n-1))==0)
    printf("The number is a power of two");
    Eg:
    Consider 8
    8---> 1000
    7---> 0111
    8&7-->0000
    Therefore 8 is a power of 2!!
    Consider 12
    12---> 1100
    11---> 1011
    12&11-->1000
    Therefore 12 is not a power of two

  3. #3
    Junior Member
    Join Date
    Apr 2007
    Posts
    6
    Rep Power
    7

    Re: C language - How to write a C program to find the power of 2 in a normal way and in single step?

    #include<math.h>

    pow(2,i); // 2 raise to i......i can have any value

  4. #4
    Super Moderator sk_kireeti's Avatar
    Join Date
    Feb 2006
    Posts
    2,279
    Rep Power
    65

    Re: C language - How to write a C program to find the power of 2 in a normal way and in single step?

    1<<n;

    /* above statement is equal to 2 power n; you can change value of n to any value.
    But keep in mind that this statement abilities are restricted by the size of integer.
    So you can't try with too big value for n */

    Thanks,
    Krishna

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Age
    24
    Posts
    27
    Rep Power
    3

    Re: C language - How to write a C program to find the power of 2 in a normal way and in single step?

    1<<n dude this statement is not working.....

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Content Relevant URLs by vBSEO 3.5.1 PL1