Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:13:14+00:00 2026-06-08T09:13:14+00:00

Here is a small Program to print powers of 2 till 8. But it

  • 0

Here is a small Program to print powers of 2 till 8. But it is not quitting after 8. Please explain the reason.

#include <stdio.h>
#include <unistd.h>
int main(void)
{
        unsigned int i=1;
        while(1) {
                i = i<<1;
                printf("i = %d\n",i);
                if(i==(2^8))
                        break;
                sleep(1);
        }
        printf("Exited While loop.. \n");
        return 0;
}

The Loop is not Exiting when i = 2^8. My output is something like this:

i = 2
i = 4
i = 8
i = 16
i = 32
i = 64
i = 128
i = 256
i = 512 (Should have Exited here. But the program is continuing. Why?)
i = 1024
i = 2048
i = 4096....

EDIT :

Thanks for answering that ^ is an XOR operator. But now the below code is behaving strange. Please Explain.

#include <stdio.h>
int main(void)
{
        if((2)^8 == 1<<8) {
                printf("True.. \n");
        } else {
                printf("False..!!");
        }
        return 0;
}

The above function program prints true.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:13:15+00:00Added an answer on June 8, 2026 at 9:13 am

    In C, the ^ operator means XOR (bitwise exclusive or).

    To get 2 to the power of 8, you need to either use a loop (res *=2 in a loop), or round the pow function in math.h (note that the math.h function returns float – and therefore won’t be equal to the integer).

    The simplest method is the bitwise shift, of course.

    About the edit section:

    Welcome to the wonderful world of operator precedence.
    What happens is that == has higher precedence than ^, and therefore the conditional evaluates to 2^0, which is 2, which is true.

    To make it work, you need to add parentheses:

    if ( (2^8) == (1<<8) ) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a small program showing a problem: import Data.Fixed main = do print
here is a small peice off code that i wrote. But I am not
Here is a small program I wrote, (I am still writing it), however till
Here a small piece of code for testing and explain the problem. I have
i have a small problem here which i cannot fix,This post goes through but
I've written a small Haskell program to print the MD5 checksums of all files
Here's a small C test program to demonstrate what I'm seeing. It registers a
I've read Statics in Java are not inherited. I've a small program below which
Here are two statements from my Mathematica program. Print[ExpandAll[Chop[Integrate[Resi*Cos[7*1*\[Theta]]*Sin[(1*Pi*x)/L], {\[Theta], 0,2*Pi},{x,0, L}], 10^-6]]]; This
Here is small example: A <- c(1,1,1,1, 0, 0, 0, 2,2,2) B <- c(1,1,1,1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.