M
Mark
lets say i have a char, and i want to check if the 3rd bit is 0 or 1...
how might i do this?
thanks!
how might i do this?
thanks!
Mark said:lets say i have a char, and i want to check if the 3rd bit is 0 or 1...
how might i do this?
Mike Wahler said:Learn about the bitwise operators,
e.g. | , & , etc.
Also you should used unsigned characters for this.
Mark said:alright. thanks
this should help anyone else who cares to know
http://www.cprogramming.com/tutorial/bitwise_operators.html
Mark said:alright. thanks
this should help anyone else who cares to know
http://www.cprogramming.com/tutorial/bitwise_operators.html
Shark said:There is something about bit manipulations using #define here:
http://www.embedded.com/2000/0005/0005feat2.htm
9. Embedded systems always require the user to manipulate bits in
registers or variables. Given an integer variable a, write two code
fragments. The first should set bit 3 of a. The second should clear
bit 3 of a. In both cases, the remaining bits should be unmodified.
* Use #defines and bit masks. This is a highly portable method and is
the one that should be used. My optimal solution to this problem would
be:
#define BIT3 (0x1
<
<
3)
static int a;
void set_bit3(void) { a |= BIT3; }
void clear_bit3(void) { a &= ~BIT3; }
how does this sound!
void set_bit3(int &a) { a |= BIT3; }
? Otherwise you only can use those functions to set or clear the
bits in some static object named 'a' in this translation unit...
Victor Bazarov said:Whoa!... What's wrong with your newsreading software? And why are
you writing "0x1" instead of simply "1"? Is there a difference?
Jim said:Personally, I've always wanted to be able to do something like:
#define BIT3 0b00000100
but 0x is as close as you can get.
At least it gives the intent. Which would be the same reason he wouldn't do
#define BIT3 4
because the intent of 4 is not clear.
Mark said:well... the point of defining BIT3 as 4 would be so that it WOULD be
clear when you used "BIT3".. the definition itself doesn't need to be
quite so clear does it? anyways "BIT3" should tell you something about
your intent.
Shark said:* Use #defines and bit masks. This is a highly portable method and is
the one that should be used.
#define BIT3 (0x1<< 3)
void set_bit3(void) { a |= BIT3; }
void clear_bit3(void) { a &= ~BIT3; }
how does this sound!
Shark said:The meaning of "clear" is not clear here. Do you mean clear as in
clarity or clear as in "clear the bit"?
Clarity.
besides. if we #define BIT3 0b00000100 it assumes that we are working
on a 8-bit number, while in C++ the most basic unit (i could be wrong)
int is at least 2 bytes. With (0x1<<3) we just don't have to tell what
size primitives we are working on. At least that is my guess. Again, I
could be wrong.
Shark said:Well, once again you are forgetting the context. That question and the
remark I posted was from an embedded programmer's perspective. They
worry too much about how many functions are defined where, how much
memory and crap are assigned.
Luke said:Please don't top-post. I don't know what you mean by "once again," nor
do I see how anything I said is any less applicable to embedded
programmers. On the contrary, defining a *single* function to handle
bit-setting logic rather than a separate function for each individual
bit is exactly the kind of economy embedded systems programmers are
concerned with.
What exactly did you think was wrong about what I said? Would you care
to comment on the relative technical merits of the two solutions?
Shark said:I made the original quote from this link:
http://www.embedded.com/2000/0005/0005feat2.htm
So basically
I've been corrected a couple of times for something I referenced from
another site I only know that the question was aimed at embedded
programmers and maybe someone who actually works as one can tell why
you'd write a macro to set some bit.
First, I didn't really say you were wrong. I wanted to say that the
posting I made wasn't really a general solution, but was an answer to a
question. The question made a specific demand.: write two functions
that do the following, and the given answer satisfied the requirement.
So I was only pointing out the context.
Regarding the relative merits, I'd like to run away from the battle so
I can fight another day.
Luke said:Ah, no wonder. It's not even a C++ article, it's explicitly a C
article. How embarassing!
Well, you referenced it; presumably you thought it was somehow
relevant/helpful.
I could get on your case for disregarding the *relevant* context (a C++
newsgroup, the OP's question etc.), but... nah....
Shark said:If you can explicitly set the 3rd bit of the char's copy, and then
compare the result with the original then you can determine if the 3rd
bit is set or not.
int check3rdbit(char a)
I thought that should be obvious So I wasn't off topic. Plus, if its
explicitly C, what makes it not C++? after all that page wasn't
discussing implicit void* casts to char* and like!!!
haha. "Quien en tiempo huye, en tiempo acude."
Luke said:Yes, but the point is that it's just silly to hard-code '3' into the
function. What happens when you're interested in bit 4 and bit 7?
Write two new functions? Or write a single function that can handle
any of these cases?
Well, C is not C++. The fact that C++ has a C-like subset does not
mean that any "good C practice" is a good C++ practice. Implicit void*
casts to char are one example. Another is the use of #define for
constants. There's absolutely no reason to do that in C++.
Nobody said it was off-topic. Just lousy advice.
Meep!
Vad sa du?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.