bit operations

A

Ajay

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.
 
A

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.
 
W

WittyGuy

Ajay said:
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-
 
C

Chris Theis

Ajay said:
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
 
V

velthuijsen

Ajay said:
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;
}
 
A

Ajay

So

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

right?
 
A

Alf P. Steinbach

* 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:

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

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.
 
A

Alf P. Steinbach

* 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>.
 
D

Default User

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
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top