bitwise operator

This is a discussion on bitwise operator within the C Programming forums, part of the General Programming Boards category; Self Deleted......

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    deleted

    Self Deleted...
    Last edited by donkey_C; 09-26-2010 at 11:55 AM. Reason: Since no one answered i guess my phrasing was not good so deleting

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by donkey_C View Post
    Hi everyone, i am new to this forum and new to C programming itself.

    i need someone to guide me with a logic.

    I have a variable say X.
    Now this has a binary value.
    say X=10110000

    and the value keeps on changing.

    i dont want to know what it is in terms of hex or decimal.
    but i want to know which bit is 1. that is i want to know which position is 1.
    i.e. in the above case bit 4 is 1 (if you count from left starting with zero)

    i tried to putting this X into another variable which is a char array to compare. but could not..
    anyone knows an efficient way to do this without resorting to a long winded way.
    any clue will be helpful
    There are many ways to do this. With an 8-bit field, I like to use unsigned char. since that is an object with 8 bits. Copy the original byte to be tested to temp variable of the same type.
    Then a "mask" can be created to test the temp byte. Remember, "or" turns bits on and
    "and" turns bits off when the mask is set up properly. A brute force method (I'm assuming you're new at C) would be to create 8 masks to test each bit position. Set up a loop to test the result of each of the 8 mask operations. When you get some code and it works, try
    to pulling it out of main() and into its own function.

    EDIT: Clarification - Use the mask to turn off the bits not being tested, then read the variable to check if the
    bit was on or off.

    Edit#2: 8 masks is a bit too much "brute force" for me, but I suggested it that way to give you a clear understanding. A better
    way would be to have ONE mask, and then increment the bit being tested i.e change it to test for byte values 1, 2, 4, etc.
    Last edited by Char*Pntr; 09-26-2010 at 12:44 PM. Reason: my usual typos

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Quote Originally Posted by Char*Pntr View Post
    There are many ways to do this. With an 8-bit field, I like to use unsigned char. since that is an object with 8 bits. Copy the original byte to be tested to temp variable of the same type.
    Then a "mask" can be created to test the temp byte. Remember, "or" turns bits on and
    "and" turns bits off when the mask is set up properly. A brute force method (I'm assuming you're new at C) would be to create 8 masks to test each bit position. Set up a loop to test the result of each of the 8 mask operations. When you get some code and it works, try
    to pulling it out of main() and into its own function.
    Thank you very much i thought my question was not a valid one so had deleted it.
    Appreciate ur help. i will try it

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by donkey_C View Post
    Thank you very much i thought my question was not a valid one so had deleted it.
    Appreciate ur help. i will try it
    NP, we can do this! (I'm very thankful that my first C program was "Hello World"
    and not stuff like this.

    While I'm at it, I'd like to announce to the world that I'm now reading
    K/R The C Programming Language, 2nd addition!

    No longer torture this board with questions because of misinformation from
    the "learn C 21 Days"....

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Quote Originally Posted by Char*Pntr View Post
    There are many ways to do this. With an 8-bit field, I like to use unsigned char. since that is an object with 8 bits. Copy the original byte to be tested to temp variable of the same type.
    Then a "mask" can be created to test the temp byte. Remember, "or" turns bits on and
    "and" turns bits off when the mask is set up properly. A brute force method (I'm assuming you're new at C) would be to create 8 masks to test each bit position. Set up a loop to test the result of each of the 8 mask operations. When you get some code and it works, try
    to pulling it out of main() and into its own function.

    EDIT: Clarification - Use the mask to turn off the bits not being tested, then read the variable to check if the
    bit was on or off.

    Edit#2: 8 masks is a bit too much "brute force" for me, but I suggested it that way to give you a clear understanding. A better
    way would be to have ONE mask, and then increment the bit being tested i.e change it to 1, 2, 4, etc.
    sorry for troubling again,
    but in my situation the variable X is not in my control. it keeps on changing. so i need to keep monitoring it. any one bit or all the bits may go high (1) every one millisecond. so do i have to write like this say for bit 1 mask off and check variable? bit 2 do the same bit 3 do the same... or am i getting it all wrong

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Quote Originally Posted by Char*Pntr View Post
    NP, we can do this! (I'm very thankful that my first C program was "Hello World"
    and not stuff like this.

    While I'm at it, I'd like to announce to the world that I'm now reading
    K/R The C Programming Language, 2nd addition!

    No longer torture this board with questions because of misinformation from
    the "learn C 21 Days"....
    I had learnt c programming, only printf and scanf stuff ages ago. never used it for any application. an electrical engineer is better off handling high voltage stuff. overconfidence put me in this job( trying to program an embedded board ) stupid me. but right now i have no choice so struggling with it..
    Thanks for helping out

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by donkey_C View Post
    sorry for troubling again,
    but in my situation the variable X is not in my control. it keeps on changing. so i need to keep monitoring it. any one bit or all the bits may go high (1) every one millisecond. so do i have to write like this say for bit 1 mask off and check variable? bit 2 do the same bit 3 do the same... or am i getting it all wrong
    I looked at what I wrote above, the mask should be used to turn off all bits except
    for the bit position being tested for.
    Sorry, I'm still waking up!

    First you have to write some code, using code tags. Here is a useful link
    on the FAQ here: Cprogramming.com FAQ

    Check out the "How do I...?" links

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    sorry had got logged off My battery died

    Thanks ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Replies: 5
    Last Post: 10-30-2002, 09:23 PM

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