using bitwise & to convert to octal

This is a discussion on using bitwise & to convert to octal within the C Programming forums, part of the General Programming Boards category; okay, so I have no idea on how this would work. Im suppose to use convert binary to decimal then ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    using bitwise & to convert to octal

    okay, so I have no idea on how this would work. Im suppose to use convert binary to decimal then decimal to octal. i got the binary to decimal part but the decimal to octal part confuses me. the assignment says to "employ bit mask" integer which when ANDed with the original binary integer give you the corresponding octal bits) on it. The bit mask is normally generated by taking several integers 1 and left-shifting these 1s by an appropriate number of times until the bits with value 1 shifts from the least significant position to the position which corresponds to the bit we want to extract in the input binary integer. Then the result will need to be shifted right until the extracted bits arrive to the least significant position.

    Please help!! THAnks in advance

    HEres my failed attempt.....
    Code:
    void convertBase(int decimal) //Function that convert decimal to base of 8
    {
    
        char mask1[MAXBITS + 1] = {1,1,1,0,0,0};
        char mask2[MAXBITS + 1] = {0,0,0,1,1,1};
        int dec_mask1 = 0;
        int dec_mask2 = 0;
        int bit = 0;
        int weight = 1;
        int firstDigit;
        int secondDigit;
    	int result = 0; /*Where the decimal result will go*/
        
        /*Convert mask1 from binary to decimal*/
        for (bit = (MAXBITS - 1); bit >= 0; bit--, weight *= 2)
            dec_mask1 += (int)(mask1[bit] - 48)* weight;
        /*Convert mask2 to decimal*/
        for (bit = (MAXBITS - 1); bit >= 0; bit--, weight *= 2)
            dec_mask2 += (int)(mask2[bit] - 48)* weight;   
    	
        firstDigit = dec_mask1 & decimal;
        secondDigit = dec_mask2 & decimal;
        
        printf("Octal Representation of Binary Number: %d%d\n", firstDigit, secondDigit);
        
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is your name Rube Goldberg by any chance? (Rube Goldberg)

    Code:
    void convertBase(int decimal) //Function that convert decimal to base of 8
    {
        const int mask1 = (7 << 3);
        const int mask2 = (7 << 0);
        firstDigit = (decimal & mask1) + '0';
        secondDigit = (decimal & mask2) + '0';
        printf("Octal Representation of Binary Number: %d%d\n", firstDigit, secondDigit);
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 06:45 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Replies: 3
    Last Post: 08-21-2006, 07:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 11:26 AM

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