By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

bit operations

P: n/a
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.

Aug 3 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
* 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?
Aug 3 '05 #2

P: n/a
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-

Aug 3 '05 #3

P: n/a

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 '05 #4

P: n/a
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?


Aug 3 '05 #5

P: n/a
* 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?
Aug 3 '05 #6

P: n/a

"Ajay" <aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Aug 3 '05 #7

P: n/a
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.

Aug 3 '05 #8

P: n/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.


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?
Aug 3 '05 #9

P: n/a
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
Aug 3 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.