473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Right and Wrong Outputs.Language C

I wrote a program to check the max no. of consecutive 1s in binary form of given decimal no. but sometimes it is given wrong output.
For example if I enter 439 I get 4 as output but correct output is 3.

......................c
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <limits.h>
  7. #include <stdbool.h>
  8.  
  9. int main(){
  10.     int n,max=0,count=0; 
  11.     scanf("%d",&n);
  12.  
  13.     while(n!=0)
  14.     {
  15.         if(n%2!=0)
  16.          {   
  17.             count++;
  18.         }
  19.  
  20.         else
  21.          {
  22.             if(max<count)
  23.            { 
  24.                 max=count;
  25.                 count=0;
  26.             }
  27.         }
  28.         n/=2;
  29.     }
  30.     if(count>max)
  31.         max=count;
  32.  
  33.     printf("%d",max);
  34.     return 0;
  35. }
........................
Feb 24 '17 #1
2 1040
weaknessforcats
9,208 Expert Mod 8TB
I suggest you not use n /= 2. This makes n a new number with its own binary representation.

Instead make n an unsigned int to avoid int formatting (like 2's complement for negative numbers).

Then compare each bit individually:

n & 1

which will be true if the right bit of n is a 1 and false otherwise.

Then shift the bits in n to the right by 1 position

n >> 1

then when you n & 1 you are testing the second bit in the original n.

Continue in this manner until you have shfted n right by the number of bits in your unsigned int.
Feb 24 '17 #2
donbock
2,426 Expert 2GB
Shift right until the result is zero -- that way you don't have to know how many bits are in an unsigned int.
Feb 25 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: MJB | last post by:
I'm new to unicode and language issues. Basically I have a RichTextBox and I'm filling it with a string that could be any number of different languages. Sometimes the characters show up correctly...
22
by: Michael Nahas | last post by:
Antti & all interested, The draft description of my language to replace C is available at: http://nahas.is-a-geek.com/~mike/MyC.pdf I am a long time C programmer (I read the old testament...
2
by: Joachim | last post by:
When I created my C++ project I mistakenly used Chinese. Now all the labels on the buttons cannot show Swedish characters, which I would like it to do. Can I change this setting somewhere or do I...
0
by: Lloyd Dupont | last post by:
I want to use Uniscribe and at some stage one of the parameter is the default text layout order, that is: - RTL (Right To Left, ex: standart european language) - LTR (Left To Right, ex: Arabic...
0
by: sastwhc | last post by:
sastwhc wrote: > *Hi all > I try change language client side programatically.In my case farsi a > Right to Left Language,the curser direction changed by dir=rtl TAG > changed to correct direction...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
19
by: ashkaan57 | last post by:
Hi, I have a page in a right-to-left language and I am trying to make some bulleted lists using <ul>, but it puts the bullets to the left. Is there any way I can set the bullets to be on the...
26
by: hide1713 | last post by:
HI I'm currently using Python. I find that a instance variable must confined with self, for example: class a: def __init__(self): self.aa=10 def bb(self): print self.aa # See .if in c++,I...
31
by: Jim Langston | last post by:
In Python 2.5 on intel, the statement 2**2**2**2**2 evaluates to 20035299304068464649790723515602557504478254755697514192650169737108940595563114...
0
by: Gandalf | last post by:
Most of you probably speaks Latin language, so you wont understand the problem. when I try to write Hebrew in my statictext the last punctuation marks get mixed up. does someone have a solution ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.