hi
Sep 24, 2018 at 10:57am UTC
can i switch bit
Sep 24, 2018 at 11:08am UTC
Huh? Your question is meaningless. Can you explain clearly, precisely, and completely what it is you want to do?
Sep 24, 2018 at 1:21pm UTC
Flipping a bit works best with a simple xor:
0 xor 1 = 1
1 xor 1 = 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Example program
#include <iostream>
#include <cstdint>
int main()
{
// flip 2nd bit from the right, from 0.
//
uint8_t mask = (1 << 2); // 0b00000100
// 76543210
uint8_t num = 0b00001010;
num ^= mask;
std::cout << (int )num << std::endl;
}
Last edited on Sep 24, 2018 at 1:23pm UTC
Sep 24, 2018 at 1:29pm UTC
c++ supports bitwise logic operations on integer types. & is and, | is or, ^ is xor, ~ not
Sep 24, 2018 at 2:24pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <iomanip>
#include <bitset>
using namespace std;
void flipbit( unsigned &n, unsigned bit )
{
n ^= ( 1 << bit );
}
void print( unsigned n )
{
cout << setw( 12 ) << n << " " << setw( 32 ) << bitset<32>( n ) << '\n' ;
}
int main()
{
unsigned n = 0;
print( n );
// flip each bit in turn
for ( int i = 0; i < 32; i++ )
{
flipbit( n, i );
print( n );
}
// Then unflip each bit in turn
for ( int i = 31; i >= 0; i-- )
{
flipbit( n, i );
print( n );
}
}
0 00000000000000000000000000000000
1 00000000000000000000000000000001
3 00000000000000000000000000000011
7 00000000000000000000000000000111
15 00000000000000000000000000001111
31 00000000000000000000000000011111
63 00000000000000000000000000111111
127 00000000000000000000000001111111
255 00000000000000000000000011111111
511 00000000000000000000000111111111
1023 00000000000000000000001111111111
2047 00000000000000000000011111111111
4095 00000000000000000000111111111111
8191 00000000000000000001111111111111
16383 00000000000000000011111111111111
32767 00000000000000000111111111111111
65535 00000000000000001111111111111111
131071 00000000000000011111111111111111
262143 00000000000000111111111111111111
524287 00000000000001111111111111111111
1048575 00000000000011111111111111111111
2097151 00000000000111111111111111111111
4194303 00000000001111111111111111111111
8388607 00000000011111111111111111111111
16777215 00000000111111111111111111111111
33554431 00000001111111111111111111111111
67108863 00000011111111111111111111111111
134217727 00000111111111111111111111111111
268435455 00001111111111111111111111111111
536870911 00011111111111111111111111111111
1073741823 00111111111111111111111111111111
2147483647 01111111111111111111111111111111
4294967295 11111111111111111111111111111111
2147483647 01111111111111111111111111111111
1073741823 00111111111111111111111111111111
536870911 00011111111111111111111111111111
268435455 00001111111111111111111111111111
134217727 00000111111111111111111111111111
67108863 00000011111111111111111111111111
33554431 00000001111111111111111111111111
16777215 00000000111111111111111111111111
8388607 00000000011111111111111111111111
4194303 00000000001111111111111111111111
2097151 00000000000111111111111111111111
1048575 00000000000011111111111111111111
524287 00000000000001111111111111111111
262143 00000000000000111111111111111111
131071 00000000000000011111111111111111
65535 00000000000000001111111111111111
32767 00000000000000000111111111111111
16383 00000000000000000011111111111111
8191 00000000000000000001111111111111
4095 00000000000000000000111111111111
2047 00000000000000000000011111111111
1023 00000000000000000000001111111111
511 00000000000000000000000111111111
255 00000000000000000000000011111111
127 00000000000000000000000001111111
63 00000000000000000000000000111111
31 00000000000000000000000000011111
15 00000000000000000000000000001111
7 00000000000000000000000000000111
3 00000000000000000000000000000011
1 00000000000000000000000000000001
0 00000000000000000000000000000000
Last edited on Sep 24, 2018 at 2:26pm UTC
Sep 24, 2018 at 4:24pm UTC
Huh? Your question is meaningless.
Look at his bio, he's posted a load of code there.
Sep 24, 2018 at 4:33pm UTC
Guten Tag lol2020.
Was versuchst du zu tun?
Sende deinen Code.
Wir können helfen.
Sep 24, 2018 at 7:07pm UTC
Somehow I think his username says it all.
Topic archived. No new replies allowed.