473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

bit operations

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
9 2107
* 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
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

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

"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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
3
by: ThunderMusic | last post by:
Hi, I have 2 UInt64 to add and then divide the result by another value. How can I do this? because the math operators have not been defined for UInt64. Can somebody help please? Thanks ...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
13
by: Immanuel Goldstein | last post by:
Obtained under the Freedom of Information Act by the National Security Archive at George Washington University and posted on the Web today, the 74-page "Information Operations Roadmap" admits that...
36
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks!...
3
by: Hallvard B Furuseth | last post by:
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later...
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
4
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.