Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bit operations

Reply
Thread Tools

bit operations

 
 
Ajay
Guest
Posts: n/a
 
      08-03-2005
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-03-2005
* 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?
 
Reply With Quote
 
 
 
 
WittyGuy
Guest
Posts: n/a
 
      08-03-2005
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-

 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      08-03-2005

"Ajay" <> wrote in message
news: 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


 
Reply With Quote
 
velthuijsen@hotmail.com
Guest
Posts: n/a
 
      08-03-2005

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;
}

 
Reply With Quote
 
Ajay
Guest
Posts: n/a
 
      08-03-2005
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?


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-03-2005
* 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?
 
Reply With Quote
 
Ajay
Guest
Posts: n/a
 
      08-03-2005
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-03-2005
* 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?
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-03-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
"LoadLibrary" of a 32 bit so with 64 bit java on a 64 bit machine markryde@gmail.com Java 3 01-19-2007 10:30 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments