cout looping return functions

hey all,

i'm having trouble with my code....


#include <iostream>
using namespace std;

/* complie using: g++ file.cpp -o file
./file
*/

int Bit(long x, unsigned int n)
{
if(x > 0)
{
//create long sigNum instead of using x, for debugging purposes//
long sigNum = x;
n = n-1;
//shiting the bit so the most significant is the least ssignificant bit//
sigNum = sigNum >> n;
//anding the result with one to get rid of the uninportant bits//
sigNum = sigNum & 1;

if(sigNum == 1)
{
return 1;
}

else
{
return 0;
}
}

if(x = 0)
{
return 0;
}
}

int main()
{
cout << "BINARY BITWISE AND CONVERSION. \n";

long integer;
cout << "Please enter an integer: ";
cin >> integer;

unsigned int sigBit;
cout << "Please enter the bit number you are interested in:";
cin >> sigBit;

int SigBitwise = Bit(integer,sigBit);

cout << "The binary representation of the significant bit you're after is: ";
cout << SigBitwise << "\n";
cout << "The binary representation of the whole decimal is: ";

/*int binary;
for(int i = 1; i < 32; i++)
{
binary = SigBitwise(integer,i);
cout << binary;
}
*/

cout << "\n";
return 0;
}

where i have commented



for(int i = 1; i < 32; i++)
{
binary = SigBitwise(integer,i);
cout << binary;
}



i'm getting a compiling error: 'SigBitwise' cannot be used as a function.

all i want to do is keep returning the bit function so i can get my binary representation,

thanks :D
Last edited on
SigBitwise(integer,i);

This says "Call the function named SigBitwise". There is no such function. The function you made is called Bit

all i want to do is keep returning the bit function so i can get my binary representation,


If you want to use the Bit function, why are you trying to call a function named SigBitwise?
Last edited on
Try replacing
1
2
3
4
5
for(int i = 1; i < 32; i++)
{
  binary = SigBitwise(integer,i);
  cout << binary;
}


with
1
2
3
4
5
for(int i = 1; i < 32; i++)
{
  binary = Bit(integer,i);
  cout << binary;
}
ahaha, what an idiot!

thanks dude!

ok i switch the loop around as it was doing the opposite as i wanted it to do.
i.e 3 would be 110000000000000000000000000000000
instead it's now the correct 000000000000000000000000000000011

i'm still having trouble using the same method for the negative numbers!

how would i write test cases for the bit function?!

i'm really new to c++ but i've come from a background (well, i recently did a semester) of Javascript.

thanks heaps!
Last edited on
Topic archived. No new replies allowed.