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 8647199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:00:21+00:00 2026-06-12T13:00:21+00:00

Why x_temp is not updating the value, where as the commented line x &=

  • 0

Why x_temp is not updating the value, where as the commented line x &= ~(1 << i); is working perfectly.
Where it is going wrong?

int x = 0x4567;
int x_temp = 0x0;// = 0xF0FF;
int y = 0x0200;
int i;
for(i = 8; i < 12; i++)
{//clean clear
    x_temp = x & ~(1 << i);
    //x &= ~(1 << i); //This line works perfectly.
}
printf("x_temp = %x....\n", x_temp);//Still it retains the value 0x4567.
printf("x = %x....\n", x);
y = x|y; //y = x_temp|y;
printf("y = %x\n", y);
  • 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-12T13:00:22+00:00Added an answer on June 12, 2026 at 1:00 pm

    In the last iteration of your loop, i is 11, but the 11th bit of x is already 0, so the result is 0x4567. I don’t know why you expect something else. In the case of x &= ~(1 << i), you clear a bit in the previous value of x, whereas with x_temp you keep assigning a fresh value to x_temp … one case is cumulative, the other is not.

    Consider a trace of the two loops:

    for `x &= ~(1 << i)`, you have
    x is 0x4567 originally
    x is 0x4467 after clearing 1<<8
    x is 0x4467 after clearing 1<<9
    x is 0x4067 after clearing 1<<10
    x is 0x4067 after clearing 1<<11
    

    but

    for `x_temp = x & ~(1 << i)`, you have
    x is 0x4567 (originally and forever)
    x_temp is 0x4467 after clearing 1<<8 from x (which hasn't changed)
    x_temp is 0x4567 after clearing 1<<9 from x (which hasn't changed)
    x_temp is 0x4167 after clearing 1<<10 from x (which hasn't changed)
    x_temp is 0x4567, after clearing 1<<11 from x (which hasn't changed)
    

    Maybe this is clearer:
    Suppose x = 5; then a loop that sets x += 1 will yield values of 6,7,8,9,10, …
    but a loop that sets x_temp = x + 1 will yield values of 6,6,6,6,6,…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

** STILL NOT WORKING ** I am using below formula to move the ball
First time using MPI outside some simple practice apps, and something's not going right.
Whoopee, not working on that socket library for the moment. I'm trying to educate
I'm running a resque job which contains following line: temp = %x(/usr/bin/java -jar /usr/local/tika/tika-app-1.0.jar
x = abcdefg x = x.match(/ab(?:cd)ef/) shouldn't x be abef? it is not, it
class X { int i; public: X() { i = 0; } void set(int
I am currently working on a simple Scrabble implementation for a college project. I
namespace pairDemo{ template<typename L, typename R> class pair{ public: pair(const L& left,const R& right)
I have a class, class MyClass { private int val; public static final MyClass
Java is pass by value. What if I need a pass by reference. For

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.