# bit operations

Discussion in 'C++' started by Ajay, Aug 3, 2005.

1. ### AjayGuest

Hi All

Well then.. it could also be posted to C group... but I am subscribed
to this group so thought of posting here.

1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
on?
2. Given a number X, how can you check if nth bit is on or not? (You
can not destroy the number)

It is not colleger assignment. Working for last 7 years.. ... today
we were discussing about storing the array traversal & storing the
number of element we are interested in most efficient way. Somebody
suggested - use another array (but that menas too much memory). so we
thought of having a number num=0 in the beginning and and the which
ever element we are interested in we'll just store the location using
that numbered bit in the array.

Array will have at most 32 elements.

Ajay, Aug 3, 2005

2. ### Alf P. SteinbachGuest

* Ajay:
>
> Well then.. it could also be posted to C group... but I am subscribed
> to this group so thought of posting here.
>
> 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> on?
> 2. Given a number X, how can you check if nth bit is on or not? (You
> can not destroy the number)
>
>
> It is not colleger assignment. Working for last 7 years.. ...

In that case, get a new job.

And learn to spell.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Aug 3, 2005

3. ### WittyGuyGuest

Ajay wrote:
> 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> on?
> 2. Given a number X, how can you check if nth bit is on or not? (You
> can not destroy the number)

Do you know the value of n? If n is known then blindly do bitwise OR
operation leaving other bits as 0. To check do AND operation and check
for the resulting value.

As simple as eating cheese ;-)

-Wg-

WittyGuy, Aug 3, 2005
4. ### Chris TheisGuest

"Ajay" <> wrote in message
news:...
> Hi All
>
> Well then.. it could also be posted to C group... but I am subscribed
> to this group so thought of posting here.
>
> 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> on?
> 2. Given a number X, how can you check if nth bit is on or not? (You
> can not destroy the number)
>
>
> It is not colleger assignment. Working for last 7 years.. ... today
> we were discussing about storing the array traversal & storing the
> number of element we are interested in most efficient way. Somebody
> suggested - use another array (but that menas too much memory). so we
> thought of having a number num=0 in the beginning and and the which
> ever element we are interested in we'll just store the location using
> that numbered bit in the array.
>
> Array will have at most 32 elements.
>

You can use the binary OR to set a bit like this:

X |= ( 1 << BitNr );

If you want to clear it you can do this via:

X = X &~( 1 << BitNr );

To test whether the bit is set you will use the binary AND:

if( X & ( 1 << BitNr ) )
return true;

Howevery you should make sure that BitNr represents a reasonable value.

On the other hand you could also use a vector<bool> for a dynamically
growing bit array or a bitset for fixed-sized arrays of bits.

HTH
Chris

Chris Theis, Aug 3, 2005
5. ### Guest

Ajay wrote:
> Hi All
>
> Well then.. it could also be posted to C group... but I am subscribed
> to this group so thought of posting here.
>
> 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> on?

I assum you meant 1 < = n < = 32

int Shift = 1;
Shift <<= (N-1);
X |= Shift;

> 2. Given a number X, how can you check if nth bit is on or not? (You
> can not destroy the number)

int Shift = 1;
Shift <<= (N-1);
if ( 0 != (X & Shift))
{
std::cout << "Bit " << N << " is on" << std::endl;
}

, Aug 3, 2005
6. ### AjayGuest

So

if (person in your company someone makes a spelling mistake && has a
bug against his name)
fire that person
endif

right?

Alf P. Steinbach wrote:
> * Ajay:
> >
> > Well then.. it could also be posted to C group... but I am subscribed
> > to this group so thought of posting here.
> >
> > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> > on?
> > 2. Given a number X, how can you check if nth bit is on or not? (You
> > can not destroy the number)
> >
> >
> > It is not colleger assignment. Working for last 7 years.. ...

>
> In that case, get a new job.
>
> And learn to spell.
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?

Ajay, Aug 3, 2005
7. ### Alf P. SteinbachGuest

* Ajay:
> [top-posting]
> [quoting signature]

Please do not top-post in this group. See the FAQ. Corrected.

Please do not quote irrelevant material such as signatures. Corrected.

* Ajay:
> * Alf P. Steinbach:
> > * Ajay:
> > >
> > > Well then.. it could also be posted to C group... but I am subscribed
> > > to this group so thought of posting here.
> > >
> > > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> > > on?
> > > 2. Given a number X, how can you check if nth bit is on or not? (You
> > > can not destroy the number)
> > >
> > >
> > > It is not colleger assignment. Working for last 7 years.. ...

> >
> > In that case, get a new job.
> >
> > And learn to spell.

>
> So
>
> if (person in your company someone makes a spelling mistake && has a
> bug against his name)
> fire that person
> endif
>
> right?

When you haven't learned the basics after 7 years in the job, find another
job.

What I strongly suspect, however, is that you're lying, and is a high-school
student.

That's both because it seems impossible to be so incompetent, and because of
the spelling errors and behavior which indicate at most high-school level.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Aug 3, 2005
8. ### AjayGuest

Thanks everybody.. that was simple..

Btw, Alf, thanks to you too. because I really had forgotten the basics
that I should always check for correct spellings before sending any
email/group post.

about basics - sometimes a person can overlook simplest of things..
.. can heppen with anybody..

me working/not working/high school - honestly your openion does not
matter. It does not help either. Only solution would have helped here.

Ajay, Aug 3, 2005
9. ### Alf P. SteinbachGuest

* Ajay:
> Thanks everybody.. that was simple..
>
> Btw, Alf, thanks to you too. because I really had forgotten the basics
> that I should always check for correct spellings before sending any
> email/group post.
>
> about basics - sometimes a person can overlook simplest of things..
> .. can heppen with anybody..
>
> me working/not working/high school - honestly your openion does not
> matter. It does not help either. Only solution would have helped here.

The group has a policy of not doing people's homework.

For good reason: you don't learn anything that way (including that you don't
learn how to find out), and we or someone may end up with a clueless you on
a project.

See the FAQ:
<url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Aug 3, 2005
10. ### Default UserGuest

Ajay wrote:

> 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
> on?

How did you arrive at this conclusion? There are a number of integer
types in C++, none of which are guaranteed to be exactly 32 bits
(assuming you actually meant to write 1 <= N <= 32).

The built-in type int is often 32 bits, but is not required to be so.
It is required to be at least 16 bits.

Brian

Default User, Aug 3, 2005