How to implement this? | | |
Use a variable of char type and each bit of it indicates if a button is
pressed or not. For example,
0010010
tells that 3rd and 7th buttons are pressed down.
How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
set to 1 or 0?
Thanks! | | | | re: How to implement this?
"ad" <adconn@168.com> wrote in message
news:5KGbc.17317$vo5.530939@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Use a variable of char type and each bit of it indicates if a button is
> pressed or not. For example,
>
> 0010010
>
> tells that 3rd and 7th buttons are pressed down.
>
> How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
> set to 1 or 0?
>
> Thanks!
>
>[/color]
x |= (1 << n); // set bit n to 1
x &= ~(1 << n); // set bit n to 0
if (x&(1 << n)) // true if bit n is 1
john | | | | re: How to implement this?
> >[color=blue][color=green]
> > 0010010
> >
> > tells that 3rd and 7th buttons are pressed down.
> >[/color][/color]
Actually to me that looks like the 1st and 4th buttons
0000001 // zeroth button
0000010 // first button
0000100 // second button
etc.
I'm not trying to quibble semantics, but read my previous reply in light of
how I define zeroth bit, first bit, second bit etc.
john | | | | re: How to implement this?
On Sat, 03 Apr 2004 22:17:37 +0000, ad wrote:
[color=blue]
> Use a variable of char type and each bit of it indicates if a button is
> pressed or not. For example,
>
> 0010010
>
> tells that 3rd and 7th buttons are pressed down.
>
> How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit
> is set to 1 or 0?[/color]
Define the Flags for the buttons you need. Something like
#define FIRST 1
#define SECOND 2
#define THIRD 4
[...]
then you can simply do an AND with the variable you want to check, and see
wether the result is zero or not. For setting you can do an OR.
Alternatively to the defines you could produce an array which will contain
the appropriate flags.
Till
--
Please add "Salt and Peper" to the subject line to bypass my spam filter | | | | re: How to implement this?
"Till Crueger" <TillFC@gmx.net> wrote in message
news:c4ng1l$nq8$1@f1node01.rhrz.uni-
[color=blue]
> #define FIRST 1
> #define SECOND 2
> #define THIRD 4[/color]
Why not const int?
[color=blue]
> then you can simply do an AND with the variable you want to check, and see
> wether the result is zero or not. For setting you can do an OR.[/color]
How about flipping a bit? For that we can use exclusive or, for which the
symbol is ^. Please let me know if this is correct.
// suppose a byte is 4 bits, CHAR_BITS = 4
int a=0x03 // 0011
a ^= 0x01; // 0011 ^ 0001 = 0010
a ^= 0x01; // 0010 ^ 0001 = 0011 | | | | re: How to implement this?
"John Harrison" <john_andronicus@hotmail.com>:[color=blue]
> "ad" <adconn@168.com> wrote:[color=green]
> > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
> > set to 1 or 0?[/color]
>
> x |= (1 << n); // set bit n to 1
>
> x &= ~(1 << n); // set bit n to 0
>
> if (x&(1 << n)) // true if bit n is 1[/color]
These should probably be 1UL instead of 1, because of the case where
x is [unsigned] 'long' and sizeof(long) > sizeof(int). | | | | re: How to implement this?
"Till Crueger" <TillFC@gmx.net> wrote in message
news:c4ng1l$nq8$1@f1node01.rhrz.uni-
[color=blue]
> #define FIRST 1
> #define SECOND 2
> #define THIRD 4[/color]
Why not const int?
[color=blue]
> then you can simply do an AND with the variable you want to check, and see
> wether the result is zero or not. For setting you can do an OR.[/color]
How about flipping a bit? For that we can use exclusive or, for which the
symbol is ^. Please let me know if this is correct.
// suppose a byte is 4 bits, CHAR_BITS = 4
int a=0x03 // 0011
a ^= 0x01; // 0011 ^ 0001 = 0010
a ^= 0x01; // 0010 ^ 0001 = 0011 | | | | re: How to implement this?
"John Harrison" <john_andronicus@hotmail.com>:[color=blue]
> "ad" <adconn@168.com> wrote:[color=green]
> > How to set bit to to 1 or 0 in C++? How to check to see if the 6th bit is
> > set to 1 or 0?[/color]
>
> x |= (1 << n); // set bit n to 1
>
> x &= ~(1 << n); // set bit n to 0
>
> if (x&(1 << n)) // true if bit n is 1[/color]
These should probably be 1UL instead of 1, because of the case where
x is [unsigned] 'long' and sizeof(long) > sizeof(int). | | | | re: How to implement this?
Siemel Naran wrote:
[color=blue]
> "Till Crueger" <TillFC@gmx.net> wrote in message
> news:c4ng1l$nq8$1@f1node01.rhrz.uni-
>[color=green]
>> #define FIRST 1
>> #define SECOND 2
>> #define THIRD 4[/color]
>
> Why not const int?[/color]
Why not enum?
[color=blue][color=green]
>> then you can simply do an AND with the variable you want to check,
>> and see wether the result is zero or not. For setting you can do an
>> OR.[/color]
>
> How about flipping a bit? For that we can use exclusive or, for which
> the symbol is ^. Please let me know if this is correct.
>
> // suppose a byte is 4 bits, CHAR_BITS = 4[/color]
CHAR_BITS is never 4. It is at least 8.
[color=blue]
> int a=0x03 // 0011
> a ^= 0x01; // 0011 ^ 0001 = 0010
> a ^= 0x01; // 0010 ^ 0001 = 0011[/color]
Yes, that's correct. | | | | re: How to implement this?
Siemel Naran wrote:
[color=blue]
> "Till Crueger" <TillFC@gmx.net> wrote in message
> news:c4ng1l$nq8$1@f1node01.rhrz.uni-
>[color=green]
>> #define FIRST 1
>> #define SECOND 2
>> #define THIRD 4[/color]
>
> Why not const int?[/color]
Why not enum?
[color=blue][color=green]
>> then you can simply do an AND with the variable you want to check,
>> and see wether the result is zero or not. For setting you can do an
>> OR.[/color]
>
> How about flipping a bit? For that we can use exclusive or, for which
> the symbol is ^. Please let me know if this is correct.
>
> // suppose a byte is 4 bits, CHAR_BITS = 4[/color]
CHAR_BITS is never 4. It is at least 8.
[color=blue]
> int a=0x03 // 0011
> a ^= 0x01; // 0011 ^ 0001 = 0010
> a ^= 0x01; // 0010 ^ 0001 = 0011[/color]
Yes, that's correct. | | | | re: How to implement this?
Rolf Magnus wrote:[color=blue]
> Siemel Naran wrote:
>[color=green]
>>
>>// suppose a byte is 4 bits, CHAR_BITS = 4[/color]
>
>
> CHAR_BITS is never 4. It is at least 8.
>[/color]
CHAR_BITS could be 4. But CHAR_BIT (at least, the CHAR_BIT that is
#defined in <climits>) cannot be. ;)
I'm sure he knows it cannot actually be 4. I believe he was just
simplifying.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: How to implement this?
Rolf Magnus wrote:[color=blue]
> Siemel Naran wrote:
>[color=green]
>>
>>// suppose a byte is 4 bits, CHAR_BITS = 4[/color]
>
>
> CHAR_BITS is never 4. It is at least 8.
>[/color]
CHAR_BITS could be 4. But CHAR_BIT (at least, the CHAR_BIT that is
#defined in <climits>) cannot be. ;)
I'm sure he knows it cannot actually be 4. I believe he was just
simplifying.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: How to implement this?
"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:c4orb0$ohr$04$1@news.t-[color=blue]
> Siemel Naran wrote:[color=green]
> > "Till Crueger" <TillFC@gmx.net> wrote in message[/color][/color]
[color=blue][color=green][color=darkred]
> >> #define FIRST 1
> >> #define SECOND 2
> >> #define THIRD 4[/color]
> >
> > Why not const int?[/color][/color]
Sure, that makes sense too. For generic names like FIRST, I thought of ints
first. For specific names like ios::in, ios__out, I think of enums first.
Either way seems just as good for generic names.
[color=blue]
> Why not enum?
>[color=green][color=darkred]
> >> then you can simply do an AND with the variable you want to check,
> >> and see wether the result is zero or not. For setting you can do an
> >> OR.[/color]
> >
> > How about flipping a bit? For that we can use exclusive or, for which
> > the symbol is ^. Please let me know if this is correct.
> >
> > // suppose a byte is 4 bits, CHAR_BITS = 4[/color]
>
> CHAR_BITS is never 4. It is at least 8.[/color]
Right. I was simplying as Kevin points out.
[color=blue][color=green]
> > int a=0x03 // 0011
> > a ^= 0x01; // 0011 ^ 0001 = 0010
> > a ^= 0x01; // 0010 ^ 0001 = 0011[/color]
>
> Yes, that's correct.[/color]
Thanks. | | | | re: How to implement this?
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message[color=blue]
> "John Harrison" <john_andronicus@hotmail.com>:[/color]
[color=blue][color=green]
> > x |= (1 << n); // set bit n to 1
> >
> > x &= ~(1 << n); // set bit n to 0
> >
> > if (x&(1 << n)) // true if bit n is 1[/color]
>
> These should probably be 1UL instead of 1, because of the case where
> x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
Is there a promotion from int -> long in expressions like int<<long? If so,
then we could make either 1 or n into an unsigned long, right?
What about the case where x is an enum. Then how do we know what to use for
1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
unsigned long, unsigned long long if our compiler supports this. If we had
typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
to use std::bitset here? | | | | re: How to implement this?
"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:c4orb0$ohr$04$1@news.t-[color=blue]
> Siemel Naran wrote:[color=green]
> > "Till Crueger" <TillFC@gmx.net> wrote in message[/color][/color]
[color=blue][color=green][color=darkred]
> >> #define FIRST 1
> >> #define SECOND 2
> >> #define THIRD 4[/color]
> >
> > Why not const int?[/color][/color]
Sure, that makes sense too. For generic names like FIRST, I thought of ints
first. For specific names like ios::in, ios__out, I think of enums first.
Either way seems just as good for generic names.
[color=blue]
> Why not enum?
>[color=green][color=darkred]
> >> then you can simply do an AND with the variable you want to check,
> >> and see wether the result is zero or not. For setting you can do an
> >> OR.[/color]
> >
> > How about flipping a bit? For that we can use exclusive or, for which
> > the symbol is ^. Please let me know if this is correct.
> >
> > // suppose a byte is 4 bits, CHAR_BITS = 4[/color]
>
> CHAR_BITS is never 4. It is at least 8.[/color]
Right. I was simplying as Kevin points out.
[color=blue][color=green]
> > int a=0x03 // 0011
> > a ^= 0x01; // 0011 ^ 0001 = 0010
> > a ^= 0x01; // 0010 ^ 0001 = 0011[/color]
>
> Yes, that's correct.[/color]
Thanks. | | | | re: How to implement this?
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message[color=blue]
> "John Harrison" <john_andronicus@hotmail.com>:[/color]
[color=blue][color=green]
> > x |= (1 << n); // set bit n to 1
> >
> > x &= ~(1 << n); // set bit n to 0
> >
> > if (x&(1 << n)) // true if bit n is 1[/color]
>
> These should probably be 1UL instead of 1, because of the case where
> x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
Is there a promotion from int -> long in expressions like int<<long? If so,
then we could make either 1 or n into an unsigned long, right?
What about the case where x is an enum. Then how do we know what to use for
1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
unsigned long, unsigned long long if our compiler supports this. If we had
typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
to use std::bitset here? | | | | re: How to implement this?
Siemel Naran wrote:
[color=blue]
> "Old Wolf" <oldwolf@inspire.net.nz> wrote in message
>[color=green]
>>"John Harrison" <john_andronicus@hotmail.com>:[/color]
>
>[color=green][color=darkred]
>>>x |= (1 << n); // set bit n to 1
>>>
>>>x &= ~(1 << n); // set bit n to 0
>>>
>>>if (x&(1 << n)) // true if bit n is 1[/color]
>>
>>These should probably be 1UL instead of 1, because of the case where
>>x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
>
>
> Is there a promotion from int -> long in expressions like int<<long?[/color]
I don't believe so. I was reading this in the standard (well, actually
the C99 standard, so if there's a difference in C++ then this won't
apply) the other day, and basically both sides undergo the integer
promotions independently, I think. int promotes to int, so it won't change.
[color=blue]
> If so,
> then we could make either 1 or n into an unsigned long, right?[/color]
I suppose, but it's not the case (unless I'm mistaken). Probably best to
make that left-hand-side an unsigned type. The rules for shifting signed
types get a little confusing, but it boils down to undefined and
implementation-defined results in several cases (when shifting negative
values, or overflowing).
[color=blue]
>
> What about the case where x is an enum. Then how do we know what to use for
> 1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
> unsigned long, unsigned long long if our compiler supports this. If we had
> typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
> to use std::bitset here?
>[/color]
I don't know that much about enums.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: How to implement this?
Siemel Naran wrote:
[color=blue]
> "Old Wolf" <oldwolf@inspire.net.nz> wrote in message
>[color=green]
>>"John Harrison" <john_andronicus@hotmail.com>:[/color]
>
>[color=green][color=darkred]
>>>x |= (1 << n); // set bit n to 1
>>>
>>>x &= ~(1 << n); // set bit n to 0
>>>
>>>if (x&(1 << n)) // true if bit n is 1[/color]
>>
>>These should probably be 1UL instead of 1, because of the case where
>>x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
>
>
> Is there a promotion from int -> long in expressions like int<<long?[/color]
I don't believe so. I was reading this in the standard (well, actually
the C99 standard, so if there's a difference in C++ then this won't
apply) the other day, and basically both sides undergo the integer
promotions independently, I think. int promotes to int, so it won't change.
[color=blue]
> If so,
> then we could make either 1 or n into an unsigned long, right?[/color]
I suppose, but it's not the case (unless I'm mistaken). Probably best to
make that left-hand-side an unsigned type. The rules for shifting signed
types get a little confusing, but it boils down to undefined and
implementation-defined results in several cases (when shifting negative
values, or overflowing).
[color=blue]
>
> What about the case where x is an enum. Then how do we know what to use for
> 1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
> unsigned long, unsigned long long if our compiler supports this. If we had
> typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
> to use std::bitset here?
>[/color]
I don't know that much about enums.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. | | | | re: How to implement this?
"Siemel Naran" wrote:[color=blue]
> "Old Wolf" wrote:[color=green]
> > "John Harrison" wrote:[/color]
>[color=green][color=darkred]
> > > x |= (1 << n); // set bit n to 1
> > >
> > > x &= ~(1 << n); // set bit n to 0
> > >
> > > if (x&(1 << n)) // true if bit n is 1[/color]
> >
> > These should probably be 1UL instead of 1, because of the case where
> > x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
>
> Is there a promotion from int -> long in expressions like int<<long? If so,
> then we could make either 1 or n into an unsigned long, right?[/color]
Nope. There is for '+', '-' etc. but not for the shift operators.
[color=blue]
> What about the case where x is an enum. Then how do we know what to use for
> 1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
> unsigned long, unsigned long long if our compiler supports this. If we had
> typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
> to use std::bitset here?[/color]
[Note - this discussion applies to the case where the above expressions
are implemented as macros (otherwise , if it's a function or direct
expression, then typeof(x) is known)]
It doesn't matter if the type is too long because the compiler
will optimise it down. Also I wonder if you could get uber-portability
with (((x)&0)+1) instead of just (1), or something. That does introduce
he side-effect of evaluating (x) twice in the testing expression though.
If we are in C++ (as the NG suggests :) ) then they can just be
implemented as template functions without much difficulty, eg.
template<typename T> void set_bit(T &x, int n) { x |= T(1) << n; }
or something similar where you pass in x and return the answer, etc.
This also lets you have some sort of verification that 'n' is in
range, if you so desire.
Finally, if x were an enum, you would probably cast it to something
before calling these functions (or not do bit manipulation
on enums :) | | | | re: How to implement this?
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
[color=blue]
> If we are in C++ (as the NG suggests :) ) then they can just be
> implemented as template functions without much difficulty, eg.
> template<typename T> void set_bit(T &x, int n) { x |= T(1) << n; }
> or something similar where you pass in x and return the answer, etc.
> This also lets you have some sort of verification that 'n' is in
> range, if you so desire.[/color]
Makes sense.
[color=blue]
> Finally, if x were an enum, you would probably cast it to something
> before calling these functions (or not do bit manipulation
> on enums :)[/color]
The central question: what is the 'something'? | | | | re: How to implement this?
"Siemel Naran" wrote:[color=blue]
> "Old Wolf" wrote:[color=green]
> > "John Harrison" wrote:[/color]
>[color=green][color=darkred]
> > > x |= (1 << n); // set bit n to 1
> > >
> > > x &= ~(1 << n); // set bit n to 0
> > >
> > > if (x&(1 << n)) // true if bit n is 1[/color]
> >
> > These should probably be 1UL instead of 1, because of the case where
> > x is [unsigned] 'long' and sizeof(long) > sizeof(int).[/color]
>
> Is there a promotion from int -> long in expressions like int<<long? If so,
> then we could make either 1 or n into an unsigned long, right?[/color]
Nope. There is for '+', '-' etc. but not for the shift operators.
[color=blue]
> What about the case where x is an enum. Then how do we know what to use for
> 1 or n? Sometimes unsigned short is appropriate, sometimes unsigned int,
> unsigned long, unsigned long long if our compiler supports this. If we had
> typeof then we'd just say { x |= (typeof(X)1 << typeof(X)n). Or do we have
> to use std::bitset here?[/color]
[Note - this discussion applies to the case where the above expressions
are implemented as macros (otherwise , if it's a function or direct
expression, then typeof(x) is known)]
It doesn't matter if the type is too long because the compiler
will optimise it down. Also I wonder if you could get uber-portability
with (((x)&0)+1) instead of just (1), or something. That does introduce
he side-effect of evaluating (x) twice in the testing expression though.
If we are in C++ (as the NG suggests :) ) then they can just be
implemented as template functions without much difficulty, eg.
template<typename T> void set_bit(T &x, int n) { x |= T(1) << n; }
or something similar where you pass in x and return the answer, etc.
This also lets you have some sort of verification that 'n' is in
range, if you so desire.
Finally, if x were an enum, you would probably cast it to something
before calling these functions (or not do bit manipulation
on enums :) | | | | re: How to implement this?
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
[color=blue]
> If we are in C++ (as the NG suggests :) ) then they can just be
> implemented as template functions without much difficulty, eg.
> template<typename T> void set_bit(T &x, int n) { x |= T(1) << n; }
> or something similar where you pass in x and return the answer, etc.
> This also lets you have some sort of verification that 'n' is in
> range, if you so desire.[/color]
Makes sense.
[color=blue]
> Finally, if x were an enum, you would probably cast it to something
> before calling these functions (or not do bit manipulation
> on enums :)[/color]
The central question: what is the 'something'? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|