473,406 Members | 2,352 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,406 software developers and data experts.

program without using mathamatical operators

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
Jun 22 '06 #1
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?
Jun 22 '06 #2
hi,
thanx for the reply,what iam saying is with out using mathematical operators not bitwise operators.
Jun 25 '06 #3
D_C
293 100+
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.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int number = 1; // assume 32-bit.
  4.   while(number != (1 << 31)) // logical shift left, bitwise operator
  5.   {
  6.     cout << number << endl;
  7.     number = (number << 1);
  8.   }
  9.       system("PAUSE");
  10.       return 0;
  11. }
Jun 25 '06 #4
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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     unsigned int number; // assume 32-bit.
  4.  
  5.     for(number=1; number != 0; number <<= 1)
  6.     {
  7.         cout << number << endl;
  8.     }
  9.     system("PAUSE");
  10.     return 0;
  11. }
Unfortunately this doesn't get you to the 100th power and the shift operators do not work on floating point types.
Jun 26 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
4
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...
1
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...
24
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...
22
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.
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
6
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...
39
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
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
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.