Thread: bitwise? am i right?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    bitwise? am i right?

    i wish to check whether the bit i in the data word "Address" is "1" or "0". So i decided to use for loop and bitwise. But I'm not sure if the code is correct. My address is 0b0110000111010110. Heres the code:

    Code:
    unsigned char a = 0b000000000001;
    for (i=0 ; i < 16 ; i++)
    {	
          //checks whether the bit i in the data word "Address" is "1" or "0"
          if (Address && a) !=0   // ==> True if Bit 1 is a logic "1", else False
         {
              // output a logic "1":
              for (j=0 ; j < 21 ; j++)
             {
    	G1_LED2	= 1;
          	DelayUs(9);
          	G1_LED2	= 0;
          	DelayUs(9);
             }
                
                     DelayUs(1690);
         }
         else
        {
             // output a logic "0"
             for (j=0 ; j < 21 ; j++)
             {
    	G1_LED2	= 1;
          	DelayUs(9);
          	G1_LED2	= 0;
          	DelayUs(9);
             }
                   
             DelayUs(560);
        }	
        a = a <<1;
        i++;
    }
    Is the above code correct?
    Im currently using MPlab and seriously, i do not know what compiler i am using. Any recommendations for compilers that i can download frm the net?
    Thanks!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're not using i in any form or fashion, so you're not checking bit i. Currently you're checking bit 31 (the LSB) a bunch of times. You need to change a/use i somehow to get different bits.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    but I thought I already use i++ and that a is updated at each time of the loop? So I thought, during the first loop when i =0, the address is being compared with 0b000000000001...and after a = a<<1, a becomes 0b000000000010. and after i++ , it goes to the second loop and the addresss will now be compared with 0b000000000010...and so on..

    sorry but correct me if I'm wrong here. And if you could give me an example, it would be best. Thanks once again.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by skiz View Post
    but I thought I already use i++ and that a is updated at each time of the loop? So I thought, during the first loop when i =0, the address is being compared with 0b000000000001...and after a = a<<1, a becomes 0b000000000010. and after i++ , it goes to the second loop and the addresss will now be compared with 0b000000000010...and so on..

    sorry but correct me if I'm wrong here. And if you could give me an example, it would be best. Thanks once again.
    No, you're right. I started to glaze over when the else looked the same as the if and didn't see it down there. So yes, a starts at the least significant bit and moves over. However you are adding to i twice (once in the control of the for-loop and once at the bottom of the loop itself).

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    However you are adding to i twice (once in the control of the for-loop and once at the bottom of the loop itself).
    Code:
    for (i=0 ; i < 16 ; i++)   <<<< u mean here?
    {     ......
           ....
           i++;                       <<<< and here?
    }
    what shd i do? hmmm..

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    oh..or do i just delete off i++? hehe...sorry i'm bad at this.

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by skiz View Post
    oh..or do i just delete off i++? hehe...sorry i'm bad at this.
    yes. Otherwise you'll end up checking only half of the bits.

    Im currently using MPlab and seriously, i do not know what compiler i am using. Any recommendations for compilers that i can download frm the net?
    Code::Blocks IDE is good. Some experts here recommend MS Visual studio too.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Well a silly question here as i got u only need to check the bits in ur address. then why do u need a at all.. and whats the purpose of this if (Address && a) !=0 ????

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    actually, i just realize my own silly mistake.
    it shd be:

    Code:
    if ((Address && a) !=0)
    {
    .....and so on...
    }
    and well, if I do not include a, how do I check the address bits? coz my program is such that if the result is 0, means the bit checked is '1' (since I AND it)..and my program needs to function such that if bit '1' is detected, it will delay for 1690us ..and if bit '0' is detected, it will delay for 560us.

    O_o I don't know if I'm making any sense to you in what I have just said. haha..

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Yes Boss got it

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    OK great! thank you tabstop and stevesmithx!!
    ...and also rits for ur concern. haha..

    but now when I build the program, it gives this error:
    Error [1347] ; 0. can't find 0x90 words (0x90 withtotal) for psect "text76" in segment "CODE" (largest unused contiguous range 0x31)

    Don't even have an idea what's that all abt. But anyway, i guess questions related to this problem should not be pose here! so thanx ppl! !

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. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. C bitwise operator exercise - more like newb killer
    By shdwsclan in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 07:02 AM
  4. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM
  5. Characters into bitwise ints
    By Code Zer0 in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2003, 08:34 AM