Remove leading zeros in a binary

This is a discussion on Remove leading zeros in a binary within the C Programming forums, part of the General Programming Boards category; Hi all ! I'm new in this forum, and also totally new in c programming. My code displays a binary ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Remove leading zeros in a binary

    Hi all !

    I'm new in this forum, and also totally new in c programming. My code displays a binary after entering an int. I would like to suppress the leading zeros... Here is the code:



    Code:
    #include <stdio.h>
    
    void printBin();
    int main()
            {
            int dec;
            printf("\n Enter a number : ");
    		scanf("%d",&dec);
    		printBin(dec);
            }
    
            void printBin(int num)
            {
                    int i;
    
                    for (i=0; i<32; i++)
                    {
                            if (num<0)
                            {
                            printf("1");
                            }
                            else
                            {
                            printf("0");
    
                            }
                            num = num << 1;
    
                    }
    
            }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use a flag:
    Code:
    void printBin(int num) {
    	int i, flag = 0;
    
    	for (i=0; i<32; i++) {
    		if (num<0) {
    			printf("1");
    			flag = 1;
    		} else if (flag){
    			printf("0");
    		}
    		num = num << 1;
    	}
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Hi MK27 !

    It works, except when I enter 0...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    0 is a special case.

    Code:
        if (!num) {
            printf("ZERO\n");
            return;
        }
    The return does not have a value because printBin() is void.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leading zero's..... ( reading int )
    By sh4k3 in forum C Programming
    Replies: 4
    Last Post: 06-12-2007, 09:03 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2006, 11:43 PM
  3. binary
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 12-05-2002, 01:46 AM
  4. Remove leading spaces/tabs
    By ylph02 in forum C Programming
    Replies: 4
    Last Post: 06-07-2002, 03:45 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21