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.

Bit rotate?

``````[B]#include[/B] <stdio.h>
[B]#include[/B] <limits.h>

[B]int[/B] [B]main[/B]([B]void[/B])
{
[B]unsigned[/B] [B]char[/B] value   = 0xAA;
[B]unsigned[/B] [B]char[/B] msb     = 1 << (CHAR_BIT - 1);
/*
* First capture the MSB to rotate back into the LSB.
*/
[B]unsigned[/B] [B]char[/B] highbit = !!(value & msb); /* !! forces result to LSB */
/*
* Then shift all the bits of the value. The MSB "drops off the end".
*/
[B]unsigned[/B] [B]char[/B] result  = value << 1;
/*
* "Rotate" in the former high bit.
*/
result |= highbit;
[B]printf[/B]("value = 0x%X, result = 0x%X\n", value, result);
/*
* Let's do that again!
*/
value   = result; /* start again with the previous result */
highbit = !!(value & msb);
result  = value << 1;
result |= highbit;
[B]printf[/B]("value = 0x%X, result = 0x%X\n", value, result);

[B]return[/B] 0;
}

/* my output
value = 0xAA, result = 0x55
value = 0x55, result = 0xAA
*/``````

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?

Out of curiosity, the code I wrote, is there a way that it could be done as I wrote?

You don't need all the looping. Bit shifting already shifts all of the bits in a value.

Using loops is really going about things wrong, but it is possible -- and more work. Something like this, maybe.

``````#include <iostream>
using namespace std;

int main()
{
unsigned short i, j, src = 0x55, dst = 0, bit;

for ( i = 0; i < 8; ++i )
{
// isolate a bit in source
bit = src & (1 << i) ? 1 : 0;
// determine position in destination
if ( i == 7 )
{
j = 0;
}
else
{
j = i + 1;
}
// put result in correct spot
dst |= bit << j;
}
dst &= 0xFF;

cout << "dst = " << hex << dst << '\n';
return 0;
}

/* my output
dst = aa
*/``````

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?

No idea. Sounds like a solution waiting for a problem.

damn this is deep, i've been studying this thread for the last hour

i have so many questions its unbelievable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.