JoBe 36 Posting Pro in Training

Why choose a different name :?:

When I change it the way you say, I get two error messages:

error C2143: syntax error : missing ';' before '+' :?:
error C2501: 'mytime' : missing storage-class or type specifiers :?:

JoBe 36 Posting Pro in Training

;) Hello ladies and gents,

Ive written this class time in wich I have to use operator+ to increase the hour with a certain amount of minutes like this:

time t0 (23, 59), t1; // t0 = 23.59
t1 = t1 + 120; // t1 = 1.59

Problem is, I can't get those 120 minutes into the operator+ :confused:

I know that the calculation part isn't correct yet, but I'll sort that out later, I first want to be able to get those 120 minutes into the class :!:
Please, do not give the solution as to how I should do the calculation, ONLY how I can get those 120 minutes into the class ;)

This is the program Ive written sofar,

class time
{
public:

	time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {}
	
	time operator+ (const time &a)
	{
		while (a.minutes > 0)
		{
			if (minutes >= 60)
			{
				minutes = a.minutes - 60;
				hours += 1;
			}
		else
			minutes +=minutes;
		}
		
		return (hours, minutes);
	}

	void print()const
	{
		cout<< "The time is now: " << hours << "Hr " << minutes << "." <<endl;
	}

private:
	int hours, minutes;
};

int main()
{
	time t0(23, 59), t1;

	t0.print();

	t1 = t0 + 120;
	t1.print();
	
	return 0;
}

Thanks for the assistance ;)

JoBe 36 Posting Pro in Training

Euh, I gave you tips and Narue gave you the solution :rolleyes:

Before you write a responce, I'd suggest you better read what someone wrote to help you :!:

JoBe 36 Posting Pro in Training

Why do you return m after the loop??

There's no need to :!:

And look at your loop, look how you defined it :!:

JoBe 36 Posting Pro in Training

Hi,

Ever tought about using the Algorithm of Euclides?

Example of getting the greatest common devider gcd(1147, 851)
Steps:
1) 1147 = 1x851 + 296
2) 851 = 2x296+259
3) 296 = 1x259 + 37
4) 259 = 7x37
Rest equals 0.
Then the gcd equals 37.

If you can translate this into C++ code, you should be able to find the solution.

If it's not clear, google for the Algorithm of Euclides!

JoBe 36 Posting Pro in Training

[HOMER] DOH :!: [/HOMER]

Oh Narue, why do you allways make it look so simple :mrgreen:

>

cout<< !!( x & ( 1U << i ) );

I understand '!!' being used to make sure that only 0 or 1 are shown.

Like in the previous thread, you said that x & 1 was used for accessing the lower bit, but why is there U mentioned behind it :?:

>But for the usual reasons, that's not as good of a solution.
Why is the other solution better then :?:

Anyway, thanks for the help ;)

JoBe 36 Posting Pro in Training

Nope, not working :!:

When I use this code:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

unsigned char bcd (int n);

int main()
{
	int value = 12;
	char x;

	x = bcd(value);

	while ( x != 0 )
	{
		cout<< !!( x & 1 );
		x >>= 1;
	}

	return 0;
}

unsigned char bcd (int n)
{

	if ( n >= 10 )
	{
		int a = n % 10;
		int b = ( n / 10 ) % 10;
		return ( b << 4 ) | a;
	}

	return n;
}

My result is this binary code: 01001 :o When it should be 0001 0010 :rolleyes:

I tried out using

!( x & 1 );

I tried out using

( x & 1 );

I tried out using

!!( x & 2 );

I tried out ... several other changes in this piece of code, they all gave different outcomes, but to be honest, I don't know WHY it happend and I don't know why or when I would use these changes because I don't know what happens with the code:?:

I also changed this

return ( b << 4 ) | c;

into this

return ( a << 4 ) | b;

wich actually returned this: 100001
Why, I have no idea :?:

So, to make it short, after all tries and tribulations :D I still haven't found the solution to my exercise …

JoBe 36 Posting Pro in Training

Well, after the additional tips you gave, I got to the point that I'm getting an output of 1010001, apparantly, I'm still missing one 0 at the back :-|

THe code I used was this:

int main()
{
	int value = 12345;
	char x;

	x = bcd(value);

	while ( x != 0 )
	{
		cout<< !!( x & 1 );
		x >>= 1;
	}

	return 0;
}

unsigned char bcd (int n)
{

	if ( n >= 10 )
	{
		int a = n % 10;
		int b = ( n / 10 ) % 10;

		return ( b << 4 ) | a;
	}

	return n;
}

So, all I have to do know is reverse the value 1010001 into '0'100 0101, correct :?:

Also, could you explain how this piece of code works:

!!( x & 1 )

>12 = 1100
I understand this Narue, because four bits can have a maxim value of 15, wich also equals the amount in hex from 0 to 9 and A to F.

>12 = 00010010
This however I don't understand :?: I mean, I understand it to be eight bits

@ Dave,
Thanks for the additional info, however I didn't try out the code you wrote because I feared I would get even more confuzed then I allready am :!:

I did include the link you gave me into my favorites and will read the article later on, thanks for that :!:

JoBe 36 Posting Pro in Training

>Not totally I hope.
That was my intention yes, but then again, I do like to try and learn this, it's just so frustrating that even when getting help from you and Dave, I STILL can't figure it out :!:

>

while ( value != 0 )
{
  cout<< !!( value & 1 );
  value >>= 1;
}

When I try this with the value 12345, I get this as a binary code: 10011100000011
Tough, the code should be 0001 0010 0011 0100 0101 :?:

>12345 -> 0001 0010 0011 0100 0101
I understand that the decimal value equals this binary code, the problem is, how in the h*ll do I get (0100 0101) into a return value (unsigned char) :mad:

JoBe 36 Posting Pro in Training

Dave and especially Narue,

Thanks for the help, but I'm giving up on this, I simply am not getting what is asked in this exercise and the pieces of code you are giving me Narue are just confusing me more and more and instead of leading me to the solution is getting me further and further away :!:

To say it as it is, I'm just not cut out for this, I hoped to learn this as a fun hobby, but actually is getting me frustrated as it's not working out as it should.

STill, you guys are top notch and I enjoyed learning from you tough I didn't remember all of it and certainly so it seems didn't understand all of it

JoBe 36 Posting Pro in Training

>Of course, your solution is way too long. You could practically paste the code I gave you into your function body and be set:

Well, I did use your code, but using it solely doesn't make the decimal number into a binary code, that's why I used the remaining code?

JoBe 36 Posting Pro in Training

Well, thanks for the help Narue,

With the code you showed, I managed to make this:

unsigned char bcd (int n);

int main()
{
	int value = 12345;
	int x;

	x = bcd(value);
	cout << x <<endl;

	return 0;
}

unsigned char bcd (int n)
{
	char binary[80], temp[80], code;
	int remain = 0, decimal = 0;
	int a, b, i, k = 0;

	if (n >= 10)
	{
		a = n % 10;
		b = (n / 10) % 10;
	}

	for (i = 0; i < 2; ++i)
	{
		if (i == 0)
			decimal = a;
			else
			decimal = b;

			do
			{
				remain = decimal % 2;
				decimal = decimal / 2;
				temp[k++] = remain + 0x30;
			}
			while (decimal > 0);

				while (k >= 0)
				{
					binary[n++] = temp[--k];
				}
				binary[n-1] = 0;             

				code = ( binary[n] << 4 ) | binary[n];
	}

	return code;
}

Problem is that alltough I'm not getting any error messages, when I debug the program and get towards this part of the program:

binary[n++] = temp[--k];

I get a message: Unhandled exception in LAOef4_6.exe: 0xC0000005: Access Violation :o

I'm assuming I did something seriously wrong here, tough it doesn't crash the program :?:

JoBe 36 Posting Pro in Training

Hi Acidburn,

Don't know if it's correct, but don't you have to write:

bool Date::testEqual(Date &someDate)
{
if (Date == someDate)

   return true;
else
   return false;
}

:?:

JoBe 36 Posting Pro in Training

Hi ladies and gents,

Next exercise is the following:

Write the function which we can declare as follows:
unsigned char bcd (int n);

The idea of this function is to store the last two decimal numbers of n into a 'binary coded decimal' byte and to return this value. So, when n would be equal to 12345, then the 4 and the 5 from this number would be encoded into the 8 bits as:

0100 0101

Now, I tried to use the code Narue gave me, to cut the decimal number into pieces like this:

int main()
{
	int a = 12345;
	int x;

	x = bcd(a);
	cout << x <<endl;

	return 0;
}

unsigned char bcd (int n)
{
	int i;
	char totvalue;
	ostringstream out;

	out<< n;
	string s = out.str();

	for (i = 1; i >= 1; --i)
	{			
		totvalue = s[i] - '0';
                          start changing decimal number into binary code...
	}
	return totvalue;
}

THis way, I can get the separate values of that decimal number and then try to change them into a binary code?
Thing is, I'm not sure this is actually the correct/best way of dealing with this?

Questions are,
1) could someone show me wich is the best way to deal with this and keeping in mind that I have to use:

unsigned char bcd (int n);

2) Problem occurs is, that how can I return two separate pieces of binary code: …

JoBe 36 Posting Pro in Training

the bit_print() function just prints an integer in binary. But the integer to be printed should come from the datecode() function, which Narue showed in his first post. This two functions together solves your problem that you stated in your very first post.

Aha, now I understand :!:

you should include climits because it has the value for CHAR_BIT. If it works without it probably u included something that does the same job, maybe iostream, i m not sure.

It must be iostream that does it since I didn't include any other headerfile.

Thanks for the help :!:

JoBe 36 Posting Pro in Training

>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?

#include <bitset>
#include <string>

cout<< bitset<16> ( 2685 ).to_string() <<endl;

;)

Now Narue, this is cruel :mrgreen:

I'm trying my outmost best, and you write this with only this part to be included into your code :mrgreen:

Is bitset a headerfile wich exists, or is it one you made yourself, I heard you can make your own headerfiles, so, that's why I'm asking.

Oh yes, it works like a charm your tinny piece of code, well, if you can still call it writing code :lol:

JoBe 36 Posting Pro in Training

Hey Asif, thanks for the additional help, tough, with the code Narue gave me it was sufficient ;)

I did try out your code aswell, worked like a charm :!:

Still, Ive got a few questions if you don't mind :?:

>use it like this:

bit_print(datecode(int day))

Why does it have to be written like that, since you don't use the datecode function?

>Also include the <climits> header and <cstdlib>
Why do I have to include these with the use of your code, it works well without it :?:

void bit_print(unsigned short a);

int main()
{
	int  day = 3;

	cout<< "The coded value equales: ";
	bit_print(day);

	cin.get();

	return 0;
}

void bit_print(unsigned short a)
{
	int i;
	int n = sizeof(short) * CHAR_BIT;
	int mask = 1 << (n -1);
	
	for(i = 1; i <= n; ++i)
	{
		putchar(((a & mask) == 0)? '0':'1');
		a <<= 1;
		if( i % CHAR_BIT == 0 && i < n)
			putchar(' ');
	}
}

Thanks for the example anyway :D

JoBe 36 Posting Pro in Training

@ Asif,

Not sure about bit fields, are they 0000 = value 0(decimal)
0001 = value 1(decimal)?

@ Narue,

Thanks Narue, I tought I had to return a binary code to main and therefore was confused in how I could get binary value into an integer, as you show in your example, that's not necessary.

In fact, I could return the value 2685 and then use the code snippet from Vegaseat to turn this integer into a binary value :D

JoBe 36 Posting Pro in Training

Hi ladies and gents,

I wanted to start a new exercise, but after reading it several times, I actually don't know what the exercise is all about :!:
The translation into English is this:

Write a function wich can be declarered as the following:

unsigned int datecode(int year, int month, int day);

The task of this function is to encode the given date into the 16 bits(the 16 right bits) of the unsigned int and deliver that as a function value. From left to right we get:
7 bits for the last two numbers of the year (<=99),
4 bits for the month (<=12)
5 bits for the day (<= 31)

Keep in account that the parameter (year) can be entered as (99) or (1999)!

Now, at first I tought that I could use the codebits wich where written by Vegaseat and Dave Sinkula, but after looking at it, it's obvious that's not the case since they do use pointers and strings :-|

Since I have to use the declaration: unsigned int datecode (int year, int month, int day);
The example of them can't be used :!:

Before I realised this, I did make this piece of code, using a huge part of Vegaseat Code Snippet: http://www.daniweb.com/code/snippet87.html
I tried to do this exercise starting with one parameter for the day wich resulted into this:

unsigned int datecode (int day);

int main()
{
	int  day = 3, value;

	value …
JoBe 36 Posting Pro in Training

> I can, and I have.
Thanks Narue, that's a detailed explanation :!:

JoBe 36 Posting Pro in Training

Thanks for the clear and informative explanation Narue :!:

>Most of the time, binary search trees are better written with recursive algorithms.
One question about this tough, could you explain what a binary search tree actually is :?:

JoBe 36 Posting Pro in Training

>Your question was confusing. ;)
:D I understand, sometimes it's difficult to write down exactly what I want to ask ;)

>The class is defined for you when you include <string>. It's a class from the standard C++ library, so all you need to do is create an object of the class:

#include <string>
std::string object;

Yep that's what the question was about Narue.

Thanks!

JoBe 36 Posting Pro in Training

My experience in the world of programming is very short. However, I think every recursive function can be transformed into an iterative version.

I know, that's what Dani wrote in her tutorial aswell ;)

But often recursive functions are easily understood(not for all though :D) and makes the code shorter.

I understand, I wrote with this exercise also a function wich didn't use recursion and I needed to write more code then with the use of recursion :!:

So unless you worry too much (and I mean too much) about the effieciency of your program you can adhere to the recursive solution.

What do you mean by this :?:

One thing to remember is that whether the choice is better or not depends on the problem at hand.

Well that's the question I asked, when do you know it's better to use RECURSION :?:

JoBe 36 Posting Pro in Training

>ostringstream is a completely different beast from basic_string. ;) basic_string is not a derived class, it stands alone. ostringstream derives from ostream and uses a basic_stringbuf instead of a basic_streambuf to do the dirty work.

Narue, Ive read this atleast 10 times and I still don't understand what this has got to do with my question concerning how the string class is defined in my small programm :-|

I'll try and ask my question differently, I understand a class to be written like this

class somenameto use
{
...
};

But, that's not the case in the program, but still you speak about a string 'class' --> wich part determines the class :?:

JoBe 36 Posting Pro in Training

Hi ladies and gents,

I made this exercise in wich recursion has to be used and it worked as it should:

void ggd(short x, short y)
{
	short a = 0;

	if (y == 0)
	{
		cout<<"The largest common divider is: "<< x <<endl;
	}

	else
	{
		a = (x% y);
		x = y;
		y = a;
		x% a != 0 ? ggd(x, y) :cout<<"The largest common divider is: "<< a <<endl;
	}
}

int main()
{
	ggd(1147, 851);

	cin.get();

	return 0;
}

Now, I read the tutorial Dany made about this and she mentions:

Although recursion should generally be avoided, there are still algorithms which can only be solved with its use.

Wich algorithms are they, can someone give me some examples :?:
I mean, when do you really know you HAVE TO USE recursion :?:

JoBe 36 Posting Pro in Training

>No, it's equal to the integral value of '0'.
Could you explain this abit more Narue, I tried to translate this into my language, but didn't get a good translation :!:

>..., and you would do well to remember that.
Because ASCII is limited you mean?

>The string class overloads the subscript operator, ...
And the string class is defined by --> ostringstream out;?

JoBe 36 Posting Pro in Training

Thanks for the help again Narue, got a few questions about it.

for (i = 2; i < s.length(); ++i)

Could you tell me why i has to be 2?
It's probably got to do with:
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0.

But, I actually don't understand why this is needed :?:

The above part is understood Narue, no further explanation is necessary!
Starting at 2, passes the 0 and . when looping the string, got that one ;)

totvalue += s[i] - '0'

I understand that using '0' (string value) is equal to the decimal amount of 48 , so, when detracting the digit for instance '1' - '0' it get's the value 1, I understand that!

But what I don't understand is that you are using an array wich is detached towards a string s, but, we never declared an array :o That's confusing for me Narue :confused:

I also still have the difficulty in seeing how a variable such as i, wich actually is just a counter, suddenly get's the values of the stringvalue.

Like 0.123

When i in the loop is 2

s[2] = 1

When i in the loop is 3

s[3] = 2

It's hard to explain what I'm trying to ask, but, how does i get's the value of the digits, does it have to …

JoBe 36 Posting Pro in Training

Thanks for the help again Narue, got a few questions about it.

for (i = 2; i < s.length(); ++i)

Could you tell me why i has to be 2?
It's probably got to do with:
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0.

But, I actually don't understand why this is needed :?:

totvalue += s[i] - '0'

I understand that using '0' (string value) is equal to the decimal amount of 48 , so, when detracting the digit for instance '1' - '0' it get's the value 1, I understand that!

But what I don't understand is that you are using an array wich is detached towards a string s, but, we never declared an array :o That's confusing for me Narue :confused:

I also still have the difficulty in seeing how a variable such as i, wich actually is just a counter, suddenly get's the values of the stringvalue.

Like 0.123

When i in the loop is 2

s[2] = 1

When i in the loop is 3

s[3] = 2

It's hard to explain what I'm trying to ask, but, how does i get's the value of the digits, does it have to do that i wich is used in the loop is not the same as i in s ?

Hope you understand.

JoBe 36 Posting Pro in Training

Hi Narue,

Ive been trying to get this exercise to work with the piece of code you gave me, but I'm stuck :!:

Ive put numbers 1, 2 & 3 left towards the code that I have trouble with

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

short amount (double val)
{
	short a = 0, i, totvalue = 0;
	ostringstream out;

	out<< val;
	string s = out.str();

	for (i = 0; i < s.length(); ++i) // Number 1
	{			
		a = s * 10; // Number 2
		s = s - a; // Number 3
		totvalue += a;
	}

	return totvalue;
}

int main()
{
	double a = 0.999;

	cout<<"Value = " << amount(a) <<endl;

	return 0;
}

1) I believe I can use this to determine the amount of digits behind the radix, correct?

2 & 3) when using this code, I get the following error C2676: binary '*' or '-': 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator

I understand it's not recognizing this operator, but, how can I get it to recognize it?

JoBe 36 Posting Pro in Training

^^^

guess what, Dude ...

LOL, since when did you became a man Narue :mrgreen:

JoBe 36 Posting Pro in Training

I also get 2 errors saying:
(37) : error C2065: 'max' : undeclared identifier
(40) : error C2065: 'min' : undeclared identifier

It means that in main, these two aren't declared!

What are they, integers, floats, an array, ...

Out of curiousity;
Tough I'm far from a programmer and I'm certain that I'll never be as good as most of you who post here, I find it hard to understand that, alltough you wrote this program, you fail to understand the simple errors that are shown to you while trying to compile.

Please, don't think that I'm trying to put you down, as I said, I'm not good at this myself, but such simple things as searching for errors while compiling certainly the ones you have are issues that you should be able to solve yourself, don't you think?

JoBe 36 Posting Pro in Training

>Because it's your project and not mine. I was just showing you the simplest way of determining how many digits of precision there are.
Got it, thanks for the help Narue :!:

JoBe 36 Posting Pro in Training

Hi Narue,

Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up,

cout<< precision ( 0.123456 ) <<endl;

This for example shows as result 6 and not 21?

Also, why do you use int n = 0, it seems you don't use it in the function?

return s.length() == 1 ? 0 : s.length() - 2;

I understand this to be a selection, but I don't understand what it's doing?


I added this piece of code into the program that I wrote

if ( n >= 1){ a = n; n -= a; a = 0;}

This is because previously when I entered a number like 1,1234 it didn't get rid of the number before the radix.
Not that it matters since it seems I'm not going to solve this exercise with the code I wrote :-|


I see you, Dave and Vegaseat became moderators, very smart from Dani to do this, you guys are a real asset towards this C++ community, congrats to you all :!:

JoBe 36 Posting Pro in Training

Hi Dave,

Changing the variable from float into double doesn't change anything, so, your hint towards:

Then you may discover...

isn' found?

I understand what your saying about sizeof(), problem is, I don't have a clue as to how to change this towards the amount of numbers behind the 0,... ( don't know the name of ',' in English)

I think (almost certain) that I have to find a way to determine how many numbers are behind the ',' problem is, I don't know how I can do that :confused:

JoBe 36 Posting Pro in Training

Thanks for the reply Vegaseat, but untill this part of exercises, there is no mentioning of using your examples in the piece of the book that Ive done yet, I'll have to go about 60 pages further before the explanation of strings is mentioned in detail so that I could try what you are suggesting :)

I'm at part 4 (classes and functions) :lol:
Strings is in part 6 ( Functions, strings en arrays) ;)

JoBe 36 Posting Pro in Training

Hi ladies and gents,

I'm trying the next exercise:

Write a function value, wich adds the decimal numbers of 'n' together and return the result of this into main? For example:

0.1234 = 10
0.01234 = 10
0.3286 = 19

What Ive got so far is this:

short amount (float n)
{
	short a = 0, i, totvalue = 0;

	for (i = 0; i < sizeof (n); i++)
	{
		a = n = (n * 10);
		n -= a;
		totvalue += a;
	}

	return totvalue;
}

int main()
{
	double a = 0.1234;

	cout<<"Value = " << amount(a) <<endl;

	return 0;
}

Problem is, when I run threw the loop in the function and arrive at the fourth digit '4', the compiler changes the number into '39997' and I get a wrong result :o

Also, is it possible to change the working of the loop using binary operators?

Thanks for the help :!:

JoBe 36 Posting Pro in Training

Thanks Dave,

Out of curiosity, the code I wrote, is there a way that it could be done as I wrote?Of course with some alterations :!:

The problem is, the book I'm following doesn't speak about several pieces of code you use. So, I never ever would have found the solution you gave me.

Thanks anyway, one more question, the last exercise uses a mathematical formula called Horner wich gives a formula like this one:

a3 x³ + a2 x² + a1 x + a0 =_ ((a3 x +a2) x + a1) x + a0
^these three are written underneith eachother.

Do you know anything about this and why it is used for, I found allready the explanation about what the formula does in my native language but no explanation what it's use is in a C++ program? Do you?

JoBe 36 Posting Pro in Training

Yep, that was the question Dave, thanks.

Ive been trying to get the next one working in wich I have to switch bit 0--> 1, 1-->2, ... , 7-->0 and each bit has to hold it's own value when it get's switched.

It isn't doing what it should do, as I thought you should divide each bit separatly

for (i = 0; i < 8; ++i)

and then cut of each bit

bit = x << (i * 1);  
bit &= 0xF;

I think my main problem is the fact to get the previous bit into the next one and especially number 7 into number 0 :?:

Here's what Ive got sofar,

int main()
{
	unsigned short i, x = 0;
	unsigned short bit, leftbit = 0;
	unsigned char mask = 1 << (CHAR_BIT - 1);

	cin>> hex >> x; cin.get();

	for (i = 0; i < 8; ++i)
	{
		bit = x << (i * 1);
		bit &= 0xF;

		while (bit)
		{
			if (bit & 1)
			{
				leftbit |= mask;
			}
			mask<<=1;
			bit <<=1;
		}
	}

	leftbit |= ( 0xFF00 &x);

	cout<< hex << leftbit <<endl;

	return 0;
}

Hope your able to help me out Dave, or, anyone else ofcourse ;)

JoBe 36 Posting Pro in Training

Yep, that did it:

int main()
{
	unsigned short i, x = 0;
	unsigned short nibble, reverse = 0;
	unsigned char mask = 1 << (CHAR_BIT - 1);

	cin>> hex >> x; cin.get();

	for (i = 0; i<2;++i)
	{
	             nibble = x << (i * 4);
		nibble &= 0xF;

		while (nibble)
		{
			if (nibble & 1)
			{
				reverse |= mask;    
		             }
			mask  >>= 1;
			nibble >>= 1;
		}
	}
	reverse |= (0xFF00 &x);

	cout<< hex << reverse <<endl;

	return 0;
}

Do have question tough.

reverse |= (0xFF00 & x);

Is it because I'm linking variable x towards 'FF00' that the compiler knows I want to hold the value of the last two nibbles?

If so,

for (i = 0; i<2;++i)

I changed 2 into 4 wich gave me the same result, does this mean that alltough I'm changing the value during the loop, the value of x seeing as it was f7 therefore remains f7?

Thanks for the help again Dave, two more binary operator exercises to go before I start tackling functions :D

JoBe 36 Posting Pro in Training

Hi Dave,

Tried it out, this is what I'm getting, the reverser as you showd is working fine, but I'm not getting the first two nibble's in front of the hexadecimal number as I should?

int main()
{
	unsigned short i, x=0, nibble, number, reverse = 0, mask = 1 << (CHAR_BIT - 1);

	cin>> hex >> x; cin.get();

	for (i = 0; i<4;++i)
	{
		if (i < 2)
		{
			nibble = x << (i * 4);
			nibble &= 0xF;

			while (nibble)
			{
				if (nibble & 1)
				{
					reverse |= mask;    
				}
				mask   >>= 1;
				nibble >>= 1;
			}
		}
			else			// this part serves for the last
			{				// two nibbles wich don't have to be altered!
				nibble = x << (i * 4);
				nibble &= 0xF;
			}
		number = reverse + nibble;	// <-- not getting the result
	}								// I should get.

	cout<< hex << reverse <<endl;

	return 0;
}

/*	when I enter: f703
	result is   : c0
	result should be: f7c0*/
JoBe 36 Posting Pro in Training

Thanks for the example Dave, Im testing it to see what it does and I partially understand what's happening.

As soon as I understand it completly, I'll try it out on the exercise I got and I'll show it!

Found this is the MSDN Library: CHAR_BIT 8 Number of bits in a char wich is related towards the <limits.h> library.

What I don't understand is why you write (-1) behind the CHAR_BIT part because you are allready changing the place by one by this unsigned short mask = 1 << (CHAR... ?

I'm puzzled :?:

JoBe 36 Posting Pro in Training

Hi, next exercise is this one: take a hexadecimal number like F703 and switch the last two hexadecimal numbers with eachother so that:

bit 0 switch with bit 7
bit 1 switch with bit 6
bit 2 switch with bit 5
bit 3 switch with bit 4


for example F703 would become F7C0.

I understand that 03 changing into C0 has to do with the fact that 03 is binary equal to --> 0000 0011 so switching these binary is 1100 0000 with C0 as result.

I have been trying to do this as instructed in the exercise using binary operators but with no result :o

I tried to use the inverter ~ because this can change a binary code from 0011 to 1100 but isn't related to exchanging bits with eachother, this one can be use only as a unary operator!

I tried to use the ^ operator since this one turns a bit into 0 when two bits are the same and into 1 when they are different like this code:

..1001
^0010
-------
..0100

I know the use of << wich can result in 0011 turning into 0110

I think that as in the previous exercise, I have to use the nible and have to cut the hexadecimal number into pieces of four bits like this (i *4) & 0xF;
But I fail to see how I can control …

JoBe 36 Posting Pro in Training

Unless someone wants to show me a new trick...

EUh :o ok, :o

Why oh why do I feel so stupid right now :cheesy:

JoBe 36 Posting Pro in Training

Hi Dave,

I allready understood this threw the exercise before this one and your explanation on that one, thanks ;)

Maybe I didn't explain it to well, so here it goes.

Using the binary operator <<, I can make sure that a certain variable is multiplied, this however is done in my example and your examples by using the + and - operator!

Question is, could this same exercise be done without the use of these two operators but still with the use of the binary operator << ?????

JoBe 36 Posting Pro in Training

Seeing as the next exercise is also about the use of binary operators I figured I'd use the same tread.

Idea is to multiply a certain number (not to large) by one hundred using the binary operator << a few times :!:

My solution:

int main()
{
	unsigned short x;

             cin>>x;cin.get();

             cout<< ((x << 7) - (x << 5) + (x << 2))<<endl;

	return 0;

Alltough this works, I was wondering If it isn't possible to get this done without adding and subtracting this and still only use one variable x?

JoBe 36 Posting Pro in Training

I think JoBe got it, folks are smart in Belgium!

Smart people here, of course, but don't think I'm one of them, certainly not in programming :cheesy:

But, must admit, it is fun to try and be smart :cheesy:

JoBe 36 Posting Pro in Training

Ok, got it now, thanks for the explanation :)

JoBe 36 Posting Pro in Training

Pfffffffffff, why do you guys and girls allways make it look so simple :mrgreen:

On a serious note, thanks a lot Dave ;)

>>Try not do so much as a one-liner: it can add to confusion and and be more error prone. Also, cout adds some fun that printf didnt when it comes to displaying the value.

Do you mean that It's best not to write to much code on one line? Maybe a stupid question, but being a non native English speaking person, I'dd like to be sure!

>>

nibble &= 0xF;

Could you explain this please, I think I understand 0xF wich equals a number in the decimal notation correct?

But why do you use the ampersand '&' , what does it do, I don't understand what happens with the nibble when you use it?
Is there a possibility to write a piece of code without using '&'?

Is the use of '&' related to the error message I got?

JoBe 36 Posting Pro in Training

Nope, can't figure it out :sad:

int main()
{
	unsigned int x, j;
	unsigned char  nibble;
	
	for (int i=0;i<4;i++)
	{
		cin>>j;cin.get();
		x+= j << (i * 4);
	}

	for (i=0;i<4;i++)
		cout<< hex << nibble = x << (i * 4) <<endl;

	return 0;

}

What I want to do is, is enter four different numbers smaller then 16 and then just print them!

One error that I keep getting is: error C2297: '<<' : illegal, right operand has type 'class ostream &(__cdecl *)(class ostream &)'

This has to do with the last loop, but don't know how to solve it, could someone please help me.

Thank you!

JoBe 36 Posting Pro in Training

Thanks,

But why does the nibble need to be a char and can't it be an integer?

Can you explain how this can be summorized: printf ( "x = %04X\n", x );

Using cout, can I write it like this: cout<< "x = " <<hex<<x<<endl; ????

Can I compare putting the numbers 3, 7, 14, 6 into a binary operator as a sort of array?