how do I shift bits

I need a program that can count by changing the digits in a binary file example:
1111 1111 to 1111 1110 then 1111 1101 then 1111 1011
How could I do this?
1111 1111 to 1111 1110 then 1111 1101 then 1111 1011

What result you want to see with this example?
I mean shift 1111 1111 to 1111 1110 then it to 1111 1101 then to 1111 1011...etc.
Last edited on
closed account (zb0S216C)
Have you ever heard of bit manipulation? It's only one of the most best features of C++, in my opinion. Here are some operations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char Flags( 0x3 );

// Remove a bit:
Flags &= ~( 0x1 );

// Add a bit:
Flags |= ( 0x1 );

// Test a bit:
if( Flags &( 0x1 ) ) { ... }

// Toggle a bit
Flags ^= ( 0x1 );

// Shift left:
Flags <<= 2;

// Shift right:
Flags >>= 2;


Edited in response to Disch's post :) Thanks for pointing them out, Disch.

Wazzak
Last edited on
fixed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char Flags( 0x3 );

// Remove a bit:
Flags &= ~( 0x1 );

// Add a bit:
Flags |= ( 0x1 );

// Test a bit:
if( Flags &( 0x1 ) ) { ... }

// Toggle a bit
Flags ^= ( 0x1 );

// Shift left:
Flags <<= 2;

// Shift right:
Flags >>= 2;
 
In your case you'll want to use a combination of the << operator and the & operator. If you can get a bit that looks like this:
 
1111 1110 

And & that bit with your number, you should get the desired result. You'll then need to shift the bit mentioned above to make it:
 
1111 1101

And repeat the process.
Thanks you all.
Topic archived. No new replies allowed.