开发者

Program to print the binary equivalent of a number without using format specifiers

Jus check out this program.Logically it seems fi开发者_如何学编程ne but its giving 000000000000000000000 for everything

#include<stdio.h>
void main()
{
  int n=25,k=32;
  printf("binary equivalent\n");
  while(k!=0)
  {
    if((n>>1&0x01)!=0)
      printf("1");
    else
     printf("0");
    k--;
  }
}


You don't ever change n.

Don't try and cram everything into one line, be a little more verbose so that things are clearer.

while(k!=0)
{
 if((n & 0x01) != 0)
  printf("1");
 else
  printf("0");
 k--;
 n >>= 1;
}


That is because you don't change n.

For n=25 we have (n>>1)=12 hence it prints zero. And since you don't change n it prints zero for all k.

You can change it in the following way:

#include
void main()
{
int n=25,k=32;
printf("binary equivalent\n");
while(k!=0)
{
 if((n & 0x01)!=0)
  printf("1");
 else
  printf("0");
 k--;
 n = n >> 1;
}
}

However it will print binary presentation in reversed form.


Your n is never getting changed:

if((n>>1&0x01)!=0)

should be

if(n & 0x01)

and add

n>>=1; after k--;

Also this will produce the binary representation in reverse order.


You are not modifying n - every time you compare 0x01 with second bit on n.


You don't change the value of n within the loop. And probably you want to test the least significant bit before shifting.


/*
 * Author: Andrey Vlassov
 * Date: Thu Apr 19 03:10:49 UTC 2012
 *
 * Description:
 *      An expample program demonstrating how
 *      to convert decimal integer number to
 *      binary representation
 *
 * NOTE:
 *      For simplicity additional check left out
 *
 */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    char help[] = ">>> Please provide an integer number as argument!!!";
    char id[]   = "d2b  (c) Andrey Vlassov     Apr 18, 2012 8:15PM PST";

    if( argc < 2 ) {
        printf("%s\n", help);
        exit(0);
    }

    printf("\n%s\n\n", id);

    int n   = atoi(argv[1]);

    int i, bites, bits, mask;

    printf("Number is %d\n", n);
    printf("size: %d bites\n", bites=sizeof(n));

    printf("dec: %d\n", n);
    printf("hex: %#x\n", n);
    printf("oct: %#o\n", n);
    printf("bin: b");

    bits = bites*8-1;

    mask = 0x01 << (bits-1);

    for( i=0; i<bits; i++) {
        printf("%d", ( n & mask ? 1 : 0 ) );
        mask >>= 1;
    }

    printf("\n\n");

    exit(0);
}


i think it will help the result is the same as other poster posted

#include<stdio.h>
int main()
{
int n=25;
int k=32;
printf("binary equivalent\n");

 for (int i=0;i<32;i++){
 if((n&1)!=0)
  printf("1");
 else
  printf("0");

 n>>=1;
 }


}

as @falagar said result will be printed in reverse order


// how to print binary number representation of an integer
// using bitwise operators
//
// oon
// 18.04.2013
// Refs
// http://www.cs.northwestern.edu/~wms128/bits.c
// http://www.cs.cmu.edu/~guna/15-123S11/


#include <stdio.h>

#define no_of_bits_in_a_byte 8
#define get_bit(w,i) ((w>>i)&1)

void print_binary(signed int x);

int main()
{
   print_binary(2);     // 00000000000000000000000000000010
   print_binary(-2);    // 11111111111111111111111111111110
   return 0;
}

void print_binary(signed int x)
{
   int i;
   int no_of_bytes = sizeof(x);

   for (i=no_of_bytes*no_of_bits_in_a_byte-1; i>=0; i--) {
        printf("%d",get_bit(x,i));
   }
   printf("\n");
}


/*
 * print_binary2.c
 *
 * oon
 *
 * 19.04.2013
 */

// http://www.cs.northwestern.edu/~wms128/bits.c
// http://www.cs.cmu.edu/~guna/15-123S11/

#include <stdio.h>

#define no_of_bits_in_a_byte 8
#define get_bit(w,i) ((w>>i)&1)

void print_binary2(signed int x, unsigned int n);
int check_bits_fit_in_2s_complement(signed int x, unsigned int n);

void main()
{
    print_binary2(2,2);    // output: The signed integer 2 cannot be represented by 2 bit(s) in two complements form.
    print_binary2(2,3);    // output: 010
    print_binary2(-2,2);   // output: 10
    print_binary2(-2,3);   // output: 110
}

int check_bits_fit_in_2s_complement(signed int x, unsigned int n) {
  int mask = x >> 31;

  return !(((~x & mask) + (x & ~mask))>> (n + ~0));
}

void print_binary2(signed int x, unsigned int n)
{
    // check if x can be represented by n bits in two's complement form.
    if (check_bits_fit_in_2s_complement(x,n)) {
        int i;

        for (i=n-1; i>=0; i--) {
            printf("%d",get_bit(x,i));
        }

        printf("\n");
    } else {
        printf("The signed integer %d cannot be represented by %u bit(s) in two complements form.\n",x,n);
    }
}

The above code shows how to print binary number in two's complement form where n denotes the number of bits.


int binary(int n)
{
 if(n/2)
  binary(n/2);
   printf("%d",n%2);
}
void main()
{
 int n;
   printf("enter any number");
    scanf("%d",&n);
     binary(n):
     getch();
}


Try this!

#include<iostream>
#include<stack>
using namespace std;

int main(){
    stack<int> st;
    int n=25, k=32;

    while(k!=0){
        if((n&0x01)!=0)
            st.push(1);
        else
            st.push(0);
        k--;
        n=n>>1;
    }

    while(!st.empty()){
        cout<<st.top();
        st.pop();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜