Integer to binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SteveB1007
    New Member
    • Oct 2006
    • 1

    Integer to binary

    I need help. I have to write some code converting integer to binary using bitwise and masks. I have this so far but it doesn't build.

    #include "stdafx.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    short int mask = 8;
    short int inumber;
    short int itemp = 0;

    printf("Enter a number");
    scanf("%d" ,& inumber);

    for(mask =8; inumber !=9999 && mask >0; mask>>1);
    itemp = inumber & mask;

    if (itemp == 0)
    printf("0\n");
    else
    printf(1\n");

    return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are missing the headers stdio.h and tchar.h
    You are missing a " in the second printf statement
    You have a ; following the for statement
    mask>>1 does nothing you mean mask>>=1
    As written this only outputs a single digit because there is no code block following the if statement.
    Because mask=8 this only works for values in the range 0<=inumber<=15

    Comment

    Working...