Thread: Bit manipulation

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Bit manipulation

    hi roomz


    i am writing program to do bit manipulation in DEV C++ for my project. actually i am supposed to implement in avr studio and before that i would like to watch my syntax errors and the output.

    here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
        unsigned receive_buffer1=00011111,receive_buffer2=00011111;
        unsigned m, n;
        unsigned twi_address;
        
                 //receive_buffer1 &=00000001;
                 receive_buffer1 &= 00011110;
                     m= receive_buffer1 >> 1;
                  //receive_buffer2 &= 00000001;
                  receive_buffer2 &= 00011110;
                     n= receive_buffer2<<3;
                     twi_address= n|m;
        printf("%d", twi_address);
                    //return 0;                
        }
    the output should be 11111111 but i am getting something else.

    plz help

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Do you think that's binary? Because right now, you're using a convoluted mix of octal and integer constants.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't use "binary" representation (000110) with a normal C compiler, although I believe AVR Studio has a 'b' modifier. You will need to convert your binary numbers to hexadecimal (0x06) or decimal (6) for use with Dev++.

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by jimblumberg View Post
    You can't use "binary" representation (000110) with a normal C compiler, although I believe AVR Studio has a 'b' modifier. You will need to convert your binary numbers to hexadecimal (0x06) or decimal (6) for use with Dev++.

    Jim
    so is DEV++ getting these binaries as decimal?

    then is DEV++ possess the built in compiler for hex?

    yes, AVR studio has its own compiler i suppose and i am used to write programs on it...but implementing any algorithm of my own is new for me..and that's why i wanted to check my algorithm's functionality in C!

    thank you

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by memcpy View Post
    Do you think that's binary? Because right now, you're using a convoluted mix of octal and integer constants.
    i am not sure whether DEV++ has the compiler...besides i downloaded turbo C but my keyboard is Swedish and whenever i am trying to write any code problem arises to write characters there.

    now i think i understand what is the main problem!

    thank you

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Another problem is that you're using the two crappiest compilers: Dev-C++ and Turbo C.

    You might want to look into something more reliable and recent, try Pelles C/GCC/MinGW.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    thank you all, it is working superb.

    here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
        unsigned receive_buffer1=0x1F,receive_buffer2=0x1F;
        unsigned m, n;
        unsigned twi_address;
        
                 //receive_buffer1 &=00000001;
                 receive_buffer1 &= 0x1E;
                     m= receive_buffer1 >> 1;
                  //receive_buffer2 &= 00000001;
                  receive_buffer2 &= 0x1E;
                     n= receive_buffer2<<3;
                     twi_address= n|m;
        printf("%x", twi_address);
                    //return 0;                
        }
    that was the small portion of my algorithm..

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Turbo C is horribly outdated, and has other problems. Don't ever use it for anything. Dev-C++ is also no longer maintained. Consider using the modern Code::Blocks IDE with a new version of MinGW.

    The problem you're having has to do with the leading zeros when you do something like 00011110. In C, any integer literal that starts with a zero is treated as octal (base 8). Thus, 010 does not have a value of ten, it has a value of eight. To specify a number in hexidecimal, you put a 0x before it. For example, 0x2f is 47 in decimal. These are standard features of C, every C compiler supports them, there is no need for a special "hex compiler". C does not provide a standard way to specify literals in base 2, however AVR uses the 0b prefix, so instead of 00011110, you would do something like 0b00011110. This is specific to AVR, so not all compilers will support it. Dev-C++ does not, as far as I know.

    EDIT: too slow!

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    i didn't hear about that...actually i am so hurry about my project to finish it.

    i will try it soon.

    thank you

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    Turbo C is horribly outdated, and has other problems. Don't ever use it for anything. Dev-C++ is also no longer maintained. Consider using the modern Code::Blocks IDE with a new version of MinGW.

    The problem you're having has to do with the leading zeros when you do something like 00011110. In C, any integer literal that starts with a zero is treated as octal (base 8). Thus, 010 does not have a value of ten, it has a value of eight. To specify a number in hexidecimal, you put a 0x before it. For example, 0x2f is 47 in decimal. These are standard features of C, every C compiler supports them, there is no need for a special "hex compiler". C does not provide a standard way to specify literals in base 2, however AVR uses the 0b prefix, so instead of 00011110, you would do something like 0b00011110. This is specific to AVR, so not all compilers will support it. Dev-C++ does not, as far as I know.

    EDIT: too slow!
    my back ground is not CSC. and my first programming was in 2001. after that i didn't use it. now i am doing my masters in Electronics design where C is very useful for various fields. so when i started again with C couple of months ago, i was very much used to AVR studio where i didn't have to worry about these conversion problem. in AVR studio, for bit manipulation, i can write 0x0B or something else.

    it became my habit to think like AVR studio environment or VHDL environment.

    so when i started to write my algorithm it became messy in DEV C++.

    but now i am getting use to it.

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit manipulation
    By dsupriya in forum C Programming
    Replies: 8
    Last Post: 03-05-2009, 09:36 AM
  2. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  3. set manipulation 0
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2003, 07:38 PM
  4. Bit manipulation
    By pkalluri in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 07:06 AM
  5. Help - Bit Manipulation
    By LivLuvLafLrn in forum C Programming
    Replies: 5
    Last Post: 06-06-2002, 02:47 PM