Thread: Most efficient way to read a bit from a byte?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    Most efficient way to read a bit from a byte?

    What is the most efficient way to read a bit at a particular position in a byte?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Bitwise operations.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Not sure if it's the most efficient way possible, but this is one way to do it with bitwise operators:

    Code:
    unsigned char myByte;
    ...
    myByte <<= position;
    int myBit = myByte & 1;
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    So what if I posed the same question in another forum?
    That's how you get more variety of answer from more people since there's obviously more than one C++ forum on the Internet.

    Different story if I posted the same question on the same forum..

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Tom50 View Post
    So what if I posed the same question in another forum?
    That's how you get more variety of answer from more people since there's obviously more than one C++ forum on the Internet.

    Different story if I posted the same question on the same forum..
    It's called cross-posting. To understand why it is frowned upon you must recognize that the people here are helping others voluntarily and in their own time, and so are the people on dreamincode. When cross-posting, there is a duplication of effort. My response for example was unnecessary since you already got a better and more thorough explanation in another forum, so the time i spent typing out my reply is wasted. I could've spent that time doing other things or helping someone else. Basically, you're wasting our time.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    I'm not after a single answer. I am after a variety of answers/opinions. This is a discussion board after all, and it is voluntary as you said.

    It's no different to going to the city and asking random people questions. You don't stop just because one person gave you a good/better answer, or because someone else had the same answer as a previous person, and none of the people you asked would care if they knew it.

    Giving effort to answer someones question on the Internet with the expectation that that answer is unique does not exist.

    That doesn't mean I don't appreciate your participation. In the end, that's what counts.

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Tom50 View Post
    I'm not after a single answer. I am after a variety of answers/opinions. This is a discussion board after all, and it is voluntary as you said.

    It's no different to going to the city and asking random people questions. You don't stop just because one person gave you a good/better answer, or because someone else had the same answer as a previous person, and none of the people you asked would care if they knew it.
    Bad analogy. Ask a mechanic what is wrong with your car, listen to his long and thorough reply, and then immediately ask his colleague the same question. You're insinuating that the first answer was not satisfactory, and you've wasted his time and effort, you can be certain he will not be as helpful the next time around.

    Giving effort to answer someones question on the Internet with the expectation that that answer is unique does not exist.

    That doesn't mean I don't appreciate your participation. In the end, that's what counts.
    In the end, it doesn't really matter what your opinion of crossposting is. You were told on dreamincode that it is frowned upon, and you have been told the same thing here. Do you know the expression 'biting the hand that feeds you'?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Tom50 View Post
    It's no different to going to the city and asking random people questions. You don't stop just because one person gave you a good/better answer, or because someone else had the same answer as a previous person, and none of the people you asked would care if they knew it.
    Many would.
    If you got a good answer from someone who explained it enthusiastically to you, and the next moment, in front of him...you decide to ask a random stranger....that is somewhat insulting to the first person.
    That generally means that you are not satisfied with the answer or that the answer isn't really important to you and you're asking just for the sake of asking (which seems to be the case here).

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    When I have spare time I help people on forums too, I don't question them or if they 'cross-posted' as it is irrelevant to me as they obviously asked the question for a reason. If you feel it's a 'waste of time' then please do not participate in my questions anymore.

    I had a feeling I might be needing bitwise.

    Thanks everyone for your answers. I really appreciate your help.

    I should be able to manage from here on.
    Last edited by Tom50; 03-30-2013 at 06:46 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Everyone here is just trying to give you the advice that crossposting is generally frowned upon. You may have no qualms about it, but that does not mean others do not.
    If you find a thread that is crossposted, then posting in one of those threads is your prerogative. No one is going to stop you.
    However, when you post questions, you should take into account that people do find it rude if you crosspost it, at the very least if you don't mention that you have done so (and possibly provided links). That's all we're asking.
    If you would just acknowledge that, then people will stop trying to reply further on the matter and perhaps you can get some answers to your questions (though that is doubtful considering you did crosspost).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So what if I posed the same question in another forum?
    Nothing better than starting a conversation about something you don't care you did!

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tom50 View Post
    So what if I posed the same question in another forum?
    That's how you get more variety of answer from more people since there's obviously more than one C++ forum on the Internet.

    Different story if I posted the same question on the same forum..

    You might want to follow this link for an explanation of what is happening.

    There are two significant groups of participants in a forum. The first group is made of those people who ask questions. The second group is made of up those people who answer them. The first group relies on the second. The second group, unless they are paid hard cash to answer questions, does not rely on the first.

    If people who answer questions write you off as a loser - i.e. not worth their effort to help you - they will stop helping you. One of the primary ways of getting written off as a loser is to cross-post. Why? Because, in practice, people in the second group know their counterparts on other forums.

    The way you're going, it appears this will be at least the second forum where you get written off as a loser.
    Last edited by grumpy; 03-30-2013 at 11:22 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by Tom50 View Post
    So what if I posed the same question in another forum?
    That's how you get more variety of answer from more people since there's obviously more than one C++ forum on the Internet.

    Different story if I posted the same question on the same forum..
    Try this thought experiment.

    1000 newbies randomly pick ONE forum out of 1000 and ask their question. The average number of posts per forum is therefore 1, and it will get the FULL attention of the forum residents.

    vs.

    1000 newbies all cross-post ONE question over all 1000 forums. The average number of posts per forum is now 1000, and nobody will give a ........ because each forum has been flooded, and every forum resident knows that the question is asked in many places, so there is no value in even trying to help, because the answer is likely duplicated elsewhere.

    You see, you're the thin end of the wedge that is case 2. If we tolerate your behaviour to the point that it becomes the norm, then some future self-important jerk will up the ante and post on 3 forums instead of 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read byte
    By koyboy in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2008, 12:21 PM
  2. efficient binary file read
    By Marv in forum C Programming
    Replies: 15
    Last Post: 11-19-2007, 04:42 PM
  3. Replies: 4
    Last Post: 01-30-2005, 04:50 AM
  4. Experienced coders read. ( Common/efficient writing )
    By knave in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2003, 09:07 PM
  5. Efficient way to read in a randum number of numbers
    By TrojanGekko in forum C++ Programming
    Replies: 0
    Last Post: 03-06-2002, 12:04 AM