program does not scans the number 149, and for any other number it returns 0 [closed]Why unsigned int 0xFFFFFFFF is equal to int -1?convert int to float to hexIs there any problem by accessing memory space without allocation in c languagereversing a string of integers user enters (C)Unsigned and Signed int and printfHow to swap places of a two eight-bit binary representations of an unsigned integer?Long Long int trims numbers less than it`s assumed 64bit capacity?when do printf() and scanf() functions are linked statically or dynamically to application?strtoul of negative numberin a struct, scanf returns var address not value

Stuck filament in the extruder of Infitary M508

Is it practical to use a slug-tuned variable inductor in an antenna matcher (aka "antenna tuner")?

Centering subcaptions in a tikz pgfplot subfigure environment?

Quote from Leibniz

transfer visa to new passport

Developers demotivated due to working on same project for more than 2 years

CPLD based Pierce oscillator

How can I answer high-school writing prompts without sounding weird and fake?

Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?

Why are solar panels kept tilted?

How exactly does artificial gravity work?

What's tha name for when you write multiple voices on same staff? And are there any cons?

Unexpected Netflix account registered to my Gmail address - any way it could be a hack attempt?

Extracting sublists that contain similar elements

Why was Endgame Thanos so different than Infinity War Thanos?

Why are there two different versions of the Decalogue?

Proving determinants is independent of theta

Why doesn't Rocket Lab use a solid stage?

Why did I need to *reboot* to change my group membership

On studying Computer Science vs. Software Engineering to become a proficient coder

What are the holes in files created with fallocate?

Help in identifying a mystery wall socket

German characters on US-International keyboard layout

Anabelian geometry ~ higher category theory



program does not scans the number 149, and for any other number it returns 0 [closed]


Why unsigned int 0xFFFFFFFF is equal to int -1?convert int to float to hexIs there any problem by accessing memory space without allocation in c languagereversing a string of integers user enters (C)Unsigned and Signed int and printfHow to swap places of a two eight-bit binary representations of an unsigned integer?Long Long int trims numbers less than it`s assumed 64bit capacity?when do printf() and scanf() functions are linked statically or dynamically to application?strtoul of negative numberin a struct, scanf returns var address not value






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-3















my program should be scanning a certain number (unsigned long int) and returns its odd 'turned on' indexes bits(from the binary representation of the number). but the program doesn't identify any numbers, and just returns 0, or ,does not respond at all to the number. what am i doing wrong ?



this is the fucntion:



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if ((x % 2 == 1) && (x & 1))
count++;
else
x = x << 1;


return count;



this is the main function:



int main() 

unsigned long int x;
printf("n enter a number: n");
scanf("%ul", &x);
int count_odd_bits(unsigned long int x);
printf("n the result is:%d n",count_odd_bits(x));

return 0;



for the number 149 it should return 1
(only bit number 7 is turned on)










share|improve this question















closed as off-topic by Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity Mar 23 at 15:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

    – Roland Illig
    Mar 23 at 13:10












  • Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

    – Roland Illig
    Mar 23 at 13:14






  • 2





    ... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

    – lurker
    Mar 23 at 13:16












  • Did you test 147 as well? That one also runs into an endless loop.

    – Roland Illig
    Mar 23 at 13:16






  • 3





    There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

    – Antti Haapala
    Mar 23 at 13:18


















-3















my program should be scanning a certain number (unsigned long int) and returns its odd 'turned on' indexes bits(from the binary representation of the number). but the program doesn't identify any numbers, and just returns 0, or ,does not respond at all to the number. what am i doing wrong ?



this is the fucntion:



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if ((x % 2 == 1) && (x & 1))
count++;
else
x = x << 1;


return count;



this is the main function:



int main() 

unsigned long int x;
printf("n enter a number: n");
scanf("%ul", &x);
int count_odd_bits(unsigned long int x);
printf("n the result is:%d n",count_odd_bits(x));

return 0;



for the number 149 it should return 1
(only bit number 7 is turned on)










share|improve this question















closed as off-topic by Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity Mar 23 at 15:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

    – Roland Illig
    Mar 23 at 13:10












  • Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

    – Roland Illig
    Mar 23 at 13:14






  • 2





    ... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

    – lurker
    Mar 23 at 13:16












  • Did you test 147 as well? That one also runs into an endless loop.

    – Roland Illig
    Mar 23 at 13:16






  • 3





    There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

    – Antti Haapala
    Mar 23 at 13:18














-3












-3








-3








my program should be scanning a certain number (unsigned long int) and returns its odd 'turned on' indexes bits(from the binary representation of the number). but the program doesn't identify any numbers, and just returns 0, or ,does not respond at all to the number. what am i doing wrong ?



this is the fucntion:



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if ((x % 2 == 1) && (x & 1))
count++;
else
x = x << 1;


return count;



this is the main function:



int main() 

unsigned long int x;
printf("n enter a number: n");
scanf("%ul", &x);
int count_odd_bits(unsigned long int x);
printf("n the result is:%d n",count_odd_bits(x));

return 0;



for the number 149 it should return 1
(only bit number 7 is turned on)










share|improve this question
















my program should be scanning a certain number (unsigned long int) and returns its odd 'turned on' indexes bits(from the binary representation of the number). but the program doesn't identify any numbers, and just returns 0, or ,does not respond at all to the number. what am i doing wrong ?



this is the fucntion:



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if ((x % 2 == 1) && (x & 1))
count++;
else
x = x << 1;


return count;



this is the main function:



int main() 

unsigned long int x;
printf("n enter a number: n");
scanf("%ul", &x);
int count_odd_bits(unsigned long int x);
printf("n the result is:%d n",count_odd_bits(x));

return 0;



for the number 149 it should return 1
(only bit number 7 is turned on)







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 13:14









lurker

45.7k74678




45.7k74678










asked Mar 23 at 13:05









JOHNJOHN

1




1




closed as off-topic by Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity Mar 23 at 15:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity Mar 23 at 15:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Matthieu Brucher, Hovercraft Full Of Eels, lurker, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

    – Roland Illig
    Mar 23 at 13:10












  • Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

    – Roland Illig
    Mar 23 at 13:14






  • 2





    ... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

    – lurker
    Mar 23 at 13:16












  • Did you test 147 as well? That one also runs into an endless loop.

    – Roland Illig
    Mar 23 at 13:16






  • 3





    There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

    – Antti Haapala
    Mar 23 at 13:18


















  • Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

    – Roland Illig
    Mar 23 at 13:10












  • Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

    – Roland Illig
    Mar 23 at 13:14






  • 2





    ... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

    – lurker
    Mar 23 at 13:16












  • Did you test 147 as well? That one also runs into an endless loop.

    – Roland Illig
    Mar 23 at 13:16






  • 3





    There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

    – Antti Haapala
    Mar 23 at 13:18

















Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

– Roland Illig
Mar 23 at 13:10






Before asking here for help, please always switch on all compiler warnings. For example, compile your code with gcc -Wall -Wextra -Os asdf.c. Then fix the warnings, then come back. We will happily answer your remaining questions, or any questions you have about the warnings you see. But not these trivial ones that are already answered by your compiler.

– Roland Illig
Mar 23 at 13:10














Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

– Roland Illig
Mar 23 at 13:14





Why do you expect count_odd_bits(149) to be 1? 149 is 10010101 in binary, and that means there are 4 bits set.

– Roland Illig
Mar 23 at 13:14




2




2





... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

– lurker
Mar 23 at 13:16






... else x = x << 1; I don't think you want an else here unless you want an infinite while loop once it finds a condition that's true on the if. Also you probably want >> not <<. Finally, the conditions (x % 2 == 1) and (x & 1) have the same truth value, so (x % 2 == 1) && (x & 1) is redundant.

– lurker
Mar 23 at 13:16














Did you test 147 as well? That one also runs into an endless loop.

– Roland Illig
Mar 23 at 13:16





Did you test 147 as well? That one also runs into an endless loop.

– Roland Illig
Mar 23 at 13:16




3




3





There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

– Antti Haapala
Mar 23 at 13:18






There is no conversion specifier %ul. You need %lu and you should read the compiler diagnostics

– Antti Haapala
Mar 23 at 13:18













1 Answer
1






active

oldest

votes


















2














In the function you don't change x when the if is true. So you'll end up in an endless loop.



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if ((x % 2 == 1) && (x & 1))
count++; // x not changed --> endless loop!!
else
x = x << 1;


return count;



Further it seems you need x = x >> 1; instead of the current code.



Also you don't need both x % 2 == 1 and x & 1 as they are the same.



So the code to count the number of ones could be:



int count_odd_bits(unsigned long int x) 

int count = 0;
while (x)
if (x & 1) count++;
x = x >> 1;

return count;



If you only want to count odd bit position do



int count_odd_bits(unsigned long int x) 

int count = 0;
x = x >> 1; // get rid of bit zero
while (x)
if (x & 1) count++;
x = x >> 2; // shift by 2 to get 1->3->5->....

return count;






share|improve this answer































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    In the function you don't change x when the if is true. So you'll end up in an endless loop.



    int count_odd_bits(unsigned long int x) 

    int count = 0;
    while (x)
    if ((x % 2 == 1) && (x & 1))
    count++; // x not changed --> endless loop!!
    else
    x = x << 1;


    return count;



    Further it seems you need x = x >> 1; instead of the current code.



    Also you don't need both x % 2 == 1 and x & 1 as they are the same.



    So the code to count the number of ones could be:



    int count_odd_bits(unsigned long int x) 

    int count = 0;
    while (x)
    if (x & 1) count++;
    x = x >> 1;

    return count;



    If you only want to count odd bit position do



    int count_odd_bits(unsigned long int x) 

    int count = 0;
    x = x >> 1; // get rid of bit zero
    while (x)
    if (x & 1) count++;
    x = x >> 2; // shift by 2 to get 1->3->5->....

    return count;






    share|improve this answer





























      2














      In the function you don't change x when the if is true. So you'll end up in an endless loop.



      int count_odd_bits(unsigned long int x) 

      int count = 0;
      while (x)
      if ((x % 2 == 1) && (x & 1))
      count++; // x not changed --> endless loop!!
      else
      x = x << 1;


      return count;



      Further it seems you need x = x >> 1; instead of the current code.



      Also you don't need both x % 2 == 1 and x & 1 as they are the same.



      So the code to count the number of ones could be:



      int count_odd_bits(unsigned long int x) 

      int count = 0;
      while (x)
      if (x & 1) count++;
      x = x >> 1;

      return count;



      If you only want to count odd bit position do



      int count_odd_bits(unsigned long int x) 

      int count = 0;
      x = x >> 1; // get rid of bit zero
      while (x)
      if (x & 1) count++;
      x = x >> 2; // shift by 2 to get 1->3->5->....

      return count;






      share|improve this answer



























        2












        2








        2







        In the function you don't change x when the if is true. So you'll end up in an endless loop.



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        while (x)
        if ((x % 2 == 1) && (x & 1))
        count++; // x not changed --> endless loop!!
        else
        x = x << 1;


        return count;



        Further it seems you need x = x >> 1; instead of the current code.



        Also you don't need both x % 2 == 1 and x & 1 as they are the same.



        So the code to count the number of ones could be:



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        while (x)
        if (x & 1) count++;
        x = x >> 1;

        return count;



        If you only want to count odd bit position do



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        x = x >> 1; // get rid of bit zero
        while (x)
        if (x & 1) count++;
        x = x >> 2; // shift by 2 to get 1->3->5->....

        return count;






        share|improve this answer















        In the function you don't change x when the if is true. So you'll end up in an endless loop.



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        while (x)
        if ((x % 2 == 1) && (x & 1))
        count++; // x not changed --> endless loop!!
        else
        x = x << 1;


        return count;



        Further it seems you need x = x >> 1; instead of the current code.



        Also you don't need both x % 2 == 1 and x & 1 as they are the same.



        So the code to count the number of ones could be:



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        while (x)
        if (x & 1) count++;
        x = x >> 1;

        return count;



        If you only want to count odd bit position do



        int count_odd_bits(unsigned long int x) 

        int count = 0;
        x = x >> 1; // get rid of bit zero
        while (x)
        if (x & 1) count++;
        x = x >> 2; // shift by 2 to get 1->3->5->....

        return count;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 13:35

























        answered Mar 23 at 13:17









        43864274386427

        22.5k31846




        22.5k31846















            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해