how would you print 2 powers series from 0 to 100 (like pow(2,0),pow(2,1).........pow(2,100)) with out using mathematical operators
4 2702 Banfa 9,065
Expert Mod 8TB
I wouldn't seems like a pointless exersise to me, however if you must then you need to specifiy exactly which operators and forbidden. What you the bitwise operators?
hi,
thanx for the reply,what iam saying is with out using mathematical operators not bitwise operators.
One problem you may run into is overflow. In this example, it prints 2^29, but not 2^30. Integers are 32-bit numbers, the most significant is reserved for the sign. If we include 2^30 in the loop, we shift it, 2^31 becomes a negative number, and the loop is still valid. One could always test while(number != (1<<31)). However, if the loop keeps going, since integers are typically 32-bit, (1 << 32) = 0 because the 1 overflows and is lost. - int main()
-
{
-
int number = 1; // assume 32-bit.
-
while(number != (1 << 31)) // logical shift left, bitwise operator
-
{
-
cout << number << endl;
-
number = (number << 1);
-
}
-
system("PAUSE");
-
return 0;
-
}
Banfa 9,065
Expert Mod 8TB
You should be careful using signed values in shift operations, particularly the right shift operation on a signed value is either undefined behaviour or implementation defined behaviour.
I normally try to stick to unsigned variables when using shift operators as the results are more consistent cross platform.
If you are using unsigned values you can then use 0 as the end condition of the loop -
int main()
-
{
-
unsigned int number; // assume 32-bit.
-
-
for(number=1; number != 0; number <<= 1)
-
{
-
cout << number << endl;
-
}
-
system("PAUSE");
-
return 0;
-
}
Unfortunately this doesn't get you to the 100th power and the shift operators do not work on floating point types.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Cho |
last post by:
// CHO, JOHN
#include<iostream>
class fracpri{
int whole;
int numer;
int denom;
|
by: GianGuz |
last post by:
Global new and delete operators can be overloaded to suite particulars
needs. Typically they are overloaded to insert useful debugging/trace
informations. What I would to discuss here concerns the...
|
by: REH |
last post by:
As I posted last week, I updated my C++ library to include a variant record
class. While using it in some code, I'm finding it would be convenient to
have a method that would basically say to the...
|
by: gswork |
last post by:
Let's write a c program, without knowing what it does...
Some of you may recall Jim Roger's excellent series of posts (on
comp.programming) exploring the implementation of common software...
|
by: Saurabh Saxena |
last post by:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.
|
by: raju |
last post by:
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
|
by: Protoman |
last post by:
I'm writing a program to calc truth tables of arguments to prove that
they are logically valid. Currently, I have to tell the program to
print a truth table, and check it by hand to prove it's...
|
by: mike3 |
last post by:
Hi.
I was writing a program in C++ that generates fractals. I got this
weird bug though right now that's holding it up and was wondering if
you could help.
Anyway, it seems that when this...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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,...
|
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...
| |