You are on page 1of 43

# C++ Tutorial: Quiz - Bit Manipulation - 2016

1 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

bit_byte.jpg

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

2 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

3 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

4 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

5 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## unsigned int max = ~0;

01001000 &
10111000 =
-------00001000

01001000 |
10111000 =
-------11111000

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

6 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

01110010 ^
10101010
-------11011000

00000101(5)
-------00000010(2)

>>

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

7 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

[myVariable]<<[number of places]

00001000(8) <<
-------00100000(32)

x << n
-------x * (1 << n)

8 << 3
-------8 * (1 << 3)
-------8 * (2**3)
-------64

## unsigned char a |= (1 << n);

a
1
a |= (1 << 1) = 1
a |= (1 << 3) = 1
a |= (1 << 5) = 1

0
0
0
0

0
0
0
1

0
0
0
0

0
0
1
0

0
0
0
0

0
1
0
0

0
0
0
0

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

8 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## unsigned char b &= ~(1 << n);

b
1
b &= ~(1 << 1) = 1
b &= ~(1 << 3) = 1
b &= ~(1 << 5) = 1

1
1
1
1

1
1
1
0

1
1
1
1

1
1
0
1

1
1
1
1

1
0
1
1

1
1
1
1

#include <stdio.h>
using namespace std;

/* bit clear:
1st arg: int, 2nd arg: bit position to clear */
#define CLEARBIT(a, pos) (a &= ~(1 << pos) )
int main()
{
/* 'z': decimal value 122 (=01111010) */
char a = 'z';
/* clearing the 5th bit */
char aOut = CLEARBIT(a, 5);

## /* aOut = 'Z': decimal value 90 (=01011010) */

printf("aOut=%c\n", aOut);
}

return 0;

## unsigned char c ^= (1 << n);

c
1 0 0 1 1 0 1 1
c ^= (1 << 1) = 1 0 0 1 1 0 0 1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

9 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

c ^= (1 << 3) = 1 0 0 1 0 0 1 1
c ^= (1 << 5) = 1 0 1 1 1 0 1 1

unsigned char e = d & (1 << n); //d has the byte value.

unsigned char right = val & 0xff; // right most (least significa
unsigned char left = (val>>8) & 0xff; // left most (most signifi

## bool sign = val & 0x8000; // sign bit

#include <iostream>

## using namespace std;

void binary(unsigned int n)
{
for(int i = 256; i > 0; i = i/2) {
if(n & i)
cout << " 1";
else
cout << " 0";
}
cout << endl;
}
bool getBit(int n, int index)
{
return ( (n & (1 << index) ) > 0);
}
int setBit(int n, int index, bool b)
{
if(b)

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

10 of 43

else {
}

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php
return (n | (1 << index)) ;
int mask = ~(1 << index);

int main()
{
int num, index;
num = 16;

## cout << "Input" << endl;

for (int i = 7; i >= 0; i--)
cout << getBit(num,i) << " ";
cout << endl;

/* set bit */
index = 6;
cout << "Setting " << index << "-th bit" << endl;
num = setBit(num, index, true);
for (int i = 7; i >= 0; i--)
cout << getBit(num,i) << " ";
cout << endl;
/* unset (clear) bit */
index = 4;
cout << "Unsetting (Clearing) " << index << "-th bit" <<
num = setBit(num, index, false);
for (int i = 7; i >= 0; i--)
cout << getBit(num,i) << " ";
cout << endl;
}

return 0;

Input
0 0 0 1 0 0 0 0
Setting 6-th bit
0 1 0 1 0 0 0 0
Unsetting (Clearing) 4-th bit
0 1 0 0 0 0 0 0

00000111 (7)

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

11 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

10000111 (-7)

00000111 +
10000111
-------10001110

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

12 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

00000111 +
1xxxxxxx
-------00000000

00000111 +
11111001
-------00000000

11111001

11111000 +
00000001
-------11111001

~x + 1 = -x

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

13 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## // (1111.....1111) = 2^32 - 1 = 4294967295

// = UINT_MAX (largest unsigned 32 bit integer)
unsigned int unsigned_i = -1;
int signed_i = unsigned_i;

// this becomes -1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

14 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

// 4294967295 + 2 = (2^32 - 1) + 2
// (2 ^ 32 + 1) % 32 = 1
int signed_i2 = unsigned_i + 2;

## unsigned int unsigned_i2 = unsigned_i + 2; // same as singed_i2

int signed_i_max = INT_MAX;
int signed_i_min = INT_MIN;

// INT_MAX + 1 = INT_MIN
int signed_i_overflow = INT_MAX + 1;

// No overflow

#include <iostream>
using namespace std;
int main()
{
signed char k = 0;
while(++k)
cout << static_cast<signed int>(k) << " ";
cout << endl << endl;

unsigned char uk = 0;
while(++uk)
cout << static_cast<signed int>(uk) << " ";
}

return 0;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
-128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116
-112 -111 -110 -109 -108 -107 -106 -105 -104 -103 -102 -101 -100
-95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80
-75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

15 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

-55 -54 -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40
-35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
248 249 250 251 252 253 254 255

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

16 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

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

{
int upper;
if(u < 256)
upper = 128;
else
upper = 32768;

## // check if bit is set starting from the highest bit

// (ex) upper = 128, 10000000, 01000000, 00100000, ...,
for(int i = upper; i > 0; i = i/2) {
if(u & i)
cout << "1 ";
else
cout << "0 ";
}
cout << "\n";

int main()
{
binary(5);
binary(55);
binary(255);
binary(4555);
binary(14555);
return 0;
}

5:
55:
255:
4555:
14555:

0
0
1
0
0

0
0
1
0
0

0
1
1
0
1

0
1
1
1
1

0
0
1
0
1

1
1
1
0
0

0
1
1
0
0

1
1
1
1 1 1 0 0 1 0 1 1
0 1 1 0 1 1 0 1 1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

17 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
#include <iomanip>
#include <bitset>

## void binary(unsigned int u)

{
cout << setw(5) << u << ": ";
cout << bitset<16>((int)u);
cout << "\n";
}
int main()
{
binary(5);
binary(55);
binary(255);
binary(4555);
binary(14555);
return 0;
}

5:
55:
255:
4555:
14555:

0000000000000101
0000000000110111
0000000011111111
0001000111001011
0011100011011011

void dec2bin(int n)
{
char s[100];
int i = 0;
while(n > 0) {
if(n % 2 == 0 )
s[i++] = '1';
else
s[i++] = '0';
n = n /2;
}
s[i] = '\0';
cout << s;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

18 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
#include <bitset>

## using namespace std;

int main()
{
const int BITS = sizeof(int)*CHAR_BIT;
int n = 23;

// using bitset
bitset<BITS> m(n);
cout << m << endl;

// or m = n

## // using right shift

for(int i = BITS-1; i >= 0; i--) {
cout << ((n >> i) & 1 );
}
}

return 0;

00000000000000000000000000010111
00000000000000000000000000010111

#include <iostream>
using namespace std;

## /* hex with leading zeros */

void decToHex(int n)
{
const int NIBBLES = sizeof(int)*CHAR_BIT / 4;
char *hexString = "0123456789ABCDEF";
cout << "decimal: " << n << endl;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

19 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## cout << "hex

: ";
for (int i = NIBBLES - 1; i >= 0; i--)
cout << hexString[(n >> i*4) & 0xF];
cout << endl << endl;

int main() {
decToHex(10);
decToHex(100);
decToHex(1000);
return 0;
}

decimal: 10
hex
: 0000000A
decimal: 100
hex
: 00000064
decimal: 1000
hex
: 000003E8

## // for 1000, it will pr

#include <stdio.h>

struct rgb
{
unsigned char r:2, g:2, b:2;
unsigned char:2; // this line is not needed
};
int main()
{
printf("sizeof(rgb)=%d\n", sizeof(rgb));
return 0;
}

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

20 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

sizeof(rgb)=1

#include <stdio.h>
union myColor
{
struct
{
unsigned char r:2, g:2, b:2;
};
struct
{
unsigned char rgb:6;
};
};

int main()
{
printf("sizeof(myColor)=%d\n", sizeof(myColor));
}

return 0;

sizeof(myColor)=1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

21 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
using namespace std;

## void binary(unsigned int n)

{
int upper = 255;
for(int i = upper; i > 0; i = i/2) {
if(n & i)
cout << " 1";
else
cout << " 0";
}
cout << endl;
}

## /* O(n), where n is the size, in bits, of an integer */

int bitCountA(int n)
{
int count = 0;
while(n) {
if(n & 1) count++;
n = n >> 1;
}
return count;
}

## /* O(m), where m is the number of 1's in the solution. */

int bitCountB(int n)
{
int count = 0;
while(n) {
n = n & (n-1);
count++;
}
return count;
}
int main()
{
int a;

a = 250;
binary(a);
cout << "Bit count = " << bitCountA(a) << endl;
a = 250;
binary(a);

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

22 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## cout << "Bit count = " << bitCountB(a) << endl;

return 0;

1 1 1 1 1 0 1 0
Bit count = 6
1 1 1 1 1 0 1 0
Bit count = 6

#include <iostream>

## using namespace std;

void binary(unsigned int n)
{
for(int i = 255; i > 0; i = i/2) {
if(n & i)
cout << " 1";
else
cout << " 0";
}
cout << endl;
}
int bitCount(int n)
{
int count = 0;
while(n) {
if(n & 1) count++;
n = n >> 1;
}
return count;
}

## void bitSetPos(int n, int a[])

{
int ic = 0;
for(int i = 0; i < 32; i++) {
if(n & 1) a[ic++] = i;
n = n >> 1;
}
}
int main()
{
int num = 100;
int bc, nbit;
int *a;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

23 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## /* get bit count */

nbit = bitCount(num);
/* array to store the bit set postion */
a = new int[nbit];
/* display the integer in bits */
binary(num);
/* get the position of bit set */
bitSetPos(num,a);
cout << "bit set position " << endl;
for(int i = 0; i < nbit; i++) {
cout << a[i] << " ";
}
cout << endl;
delete [] a;
}

return 0;

0 1 1 0 0 1 0 0
bit set position
2 5 6

#include <iostream>

{
a = b - a;
b = b - a;
a = a + b;
}

## // Using xor property: a^b^b => a

void inplace_swap_B(int &a, int &b)
{
a = a ^ b;
// a holds a_old ^ b_old
b = a ^ b;
// b holds a_old which is (a_old ^ b_old
a = a ^ b;
// this line means a <-- (a_old ^ b_old
// since b holds a_old.
// So, as a result, a gets the value of
}
int main()
{

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

24 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## int a = 10, b = 20;

cout << "init values:

## a = " << a << " b = " << b << end

a = 100, b = 200;
cout << "init values:

## a = " << a << " b = " << b << end

inplace_swap_A(a,b);
cout << "swap A: a = " << a << " b = " << b << endl;

inplace_swap_B(a,b);
cout << "swap B: a = " << a << " b = " << b << endl;
}

init
swap
init
swap

return 0;

values: a = 10 b = 20
A: a = 20 b = 10
values: a = 100 b = 200
B: a = 200 b = 100

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

{
int c = a ^ b;
int count = 0;

while(c != 0) {
count += c & 1;
c = c >> 1;
}
return count;

## void binary(unsigned int u)

{
int upper;
if(u < 256)
upper = 128;
else
upper = 32768;

06-02-2016 06:50 PM

25 of 43

## cout << setw(3) << u

for(int i = upper; i
if(u & i)
cout
else
cout
cout << "\n";

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## << ": ";

> 0; i = i/2)
<< "1 ";
<< "0 ";

int main()
{
int a,b;
a = 8;
b = 4;
binary(a);binary(b);
cout << "# of bit required = " << convertAtoB(a,b) << en
cout << endl;
a = 128;
b = 25;
binary(a);binary(b);
cout << "# of bit required = " << convertAtoB(a,b) << en
cout << endl;
a = 10;
b = 254;
binary(a);binary(b);
cout << "# of bit required = " << convertAtoB(a,b) << en
cout << endl;
}

return 0;

8: 0 0 0 0 1 0 0 0
4: 0 0 0 0 0 1 0 0
# of bit required = 2
128: 1 0 0 0 0 0 0 0
25: 0 0 0 1 1 0 0 1
# of bit required = 4
10: 0 0 0 0 1 0 1 0
254: 1 1 1 1 1 1 1 0
# of bit required = 5

#include <iostream>
#include <iomanip>

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

26 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

{
int upper;
if(u < 256)
upper = 128;
else
upper = 32768;

## for(int i = upper; i > 0; i = i/2)

if(u & i)
cout << "1 ";
else
cout << "0 ";
cout << "\n";

int swapEvenOddBits(int n)
{
return ( ((n & 0xaa) >> 1) | ((n & 0x55) << 1) );
}
int main()
{
int n = 180;

## cout << "Original integer" << endl;

binary(n);
cout << endl;
cout << "Ending with 0xaa" << endl;
binary(n & 0xaa);
cout << endl;
cout << "Right shift for even bits" << endl;
binary((n & 0xaa) >> 1);
cout << endl;
cout << "Ending with 0x55" << endl;
binary(n & 0x55);
cout << endl;
cout << "Left shift for odd bits" << endl;
binary((n & 0x55) << 1);
cout << endl;
cout << "Call swapEvenOddBits" << endl;
/* Swap bits */
binary( swapEvenOddBits(n)

return 0;

);

Original integer
180: 1 0 1 1 0 1 0 0
Ending with 0xaa
160: 1 0 1 0 0 0 0 0

## Right shift for even bits

80: 0 1 0 1 0 0 0 0

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

27 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## Ending with 0x55

20: 0 0 0 1 0 1 0 0

## Left shift for odd bits

40: 0 0 1 0 1 0 0 0
Call swapEvenOddBits
120: 0 1 1 1 1 0 0 0

n
= abcde1000
n-1 = abcde0111

000001000

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

## void binary(unsigned int u)

{
int upper;
if(u < 256)
upper = 128;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

28 of 43

else

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

upper = 32768;

## cout << setw(5) << u

for(int i = upper; i
if(u & i)
cout
else
cout
}
cout << "\n";

> 0; i = i/2) {
<< "1 ";
<< "0 ";

## void bit_flip(int& m, int nth)

{
m ^= 1 << nth;
}

int main()
{
int m = 12;
int nth = 2;
binary(m);
cout << "
flip " << nth << "th bit of " << m << endl;
bit_flip(m,nth);
binary(m);
cout << endl;
m = 63;
nth = 6;
binary(m);
cout << "
flip " << nth << "th bit of " << m << endl;
bit_flip(m,nth);
binary(m);

return 0;

12: 0 0 0 0 1 1 0 0
flip 2th bit of 12
8: 0 0 0 0 1 0 0 0

63: 0 0 1 1 1 1 1 1
flip 6th bit of 63
127: 0 1 1 1 1 1 1 1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

29 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

30 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

31 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
using namespace std;
/* if the dereferenced pointer is 1, the machine is little-endia
otherwise the machine is big-endian */
int endian() {
int one = 1;
char *ptr;
ptr = (char *)&one;
return (*ptr);
}

int main()
{
if(endian())
cout << "little endian\n";
else
cout << "big endian\n";
}

#include <iostream>
using namespace std;
int endian() {
union {
} endn;
}

int one;
char ch;

endn.one = 1;
return endn.ch;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

32 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

int main()
{
if(endian())
cout << "little endian\n";
else
cout << "big endian\n";
}

#include <stdio.h>
int main()
{
int a = 12345; // x00003039
char *ptr = (char*)(&a);
for(int i = 0; i < sizeof(a); i++) {
printf("%p\t0x%.2x\n", ptr+i, *(ptr+i));
}
return 0;
}

0088F8D8
0088F8D9
0088F8DA
0088F8DB

0x39
0x30
0x00
0x00

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

33 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

int i = 25;
float f = *(float*)&i;

float f = 25.0;
short s = *(short*)&f;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

34 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

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

## const int Max = 8*sizeof(int);

bool isPalindrome(int n)
{
bool palindrome = true;
bitset<Max> b = n;
for(int i = 0; i < Max/2 - 1; i++) {
if(b[i] ^ b[Max-1-i]) {
palindrome = false;
break;
}
}
return palindrome;
}

int main()
{
int m;
m = (1 << (Max-1)) + (1 << 0);
cout << bitset<Max> (m) << endl;
cout << "palindrome: " << isPalindrome(m) << endl;
m = (1 << (Max-1)) + (1 << 1);
cout << bitset<Max> (m) << endl;
cout << "palindrome: " << isPalindrome(m) << endl;
m = (1 << (Max-2)) + (1 << 1);
cout << bitset<Max> (m) << endl;
cout << "palindrome: " << isPalindrome(m) << endl;
m = (1 << (Max-6)) + (1 << 11);
cout << bitset<Max> (m) << endl;
cout << "palindrome: " << isPalindrome(m) << endl;

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

35 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## m = (1 << (Max-2)) + (1 << (Max-3)) + (1 << 2) + (1 << 1

cout << bitset<Max> (m) << endl;
cout << "palindrome: " << isPalindrome(m) << endl;
return 0;

10000000000000000000000000000001
palindrome: 1
10000000000000000000000000000010
palindrome: 0
01000000000000000000000000000010
palindrome: 1
00000100000000000000100000000000
palindrome: 0
01100000000000000000000000000110
palindrome: 1

(a)
(b)
(c)
(d)
(e)

i
i
i
i
i

& 0xFF00
>> 24
& 0xFF000000
>> (CHAR_BIT * sizeof(int) - 1))
>> (CHAR_BIT * sizeof(int) - 3))

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

36 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## // Russian Peasant Multiplication

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

## int RussianPeasant(int a, int b)

{
int x = a, y = b;
int val = 0;
cout << left << setw(5) << x << left << setw(5) << y <<
while (x > 0) {
if (x % 2 == 1) val = val + y;
y = y << 1; // double
x = x >> 1; // half
cout << left << setw(5) << x << left << setw(5)
}
return val;
}
int main() {
RussianPeasant(238, 13);
return 0;
}

238
119
59
29
14
7
3
1
0

13
26
52
104
208
416
832
1664
3328

0
0
26
78
182
182
598
1430
3094

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

37 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

## 10: ....00000000 00000000 00000000 00000010 00000000

16: ....00000000 00000000 00000000 10000010 00000000
35: ....00000100 00000000 00000000 10000010 00000000
a[1]
a[0] ->

#include <stdio.h>

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

38 of 43

#define
#define
#define
#define
int a[1

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

BITSPERWORD 32
SHIFT 5
N 100
+ N/BITSPERWORD];

## /* 1 << (i & MASK): left shift from 0 to 31, represents 2^i

i >> SHIFT: array index
0-31
a[0]
32-63 a[1]
64-95 a[2]
...
...
*/

## void setBit(int i) { a[i>>SHIFT] |= (1<<(i & MASK)); }

void clearBit(int i) { a[i>>SHIFT] &= ~(1<<(i & MASK)); }
int testBit(int i){ return a[i>>SHIFT] & (1<<(i & MASK)); }
int main(void)
{
FILE *f = fopen("integers.txt","r");
int i;
for (i = 0; i < N; i++)
clearBit(i);
printf("--input--\n");
while (fscanf(f,"%d", &i) != EOF) {
printf("%d%s", i,",");
setBit(i);
}
printf("\n--output\n");
for (i = 0; i < N; i++)
if (!testBit(i))
printf("%d%s", i,",");
}

return 0;

--input-1,3,5,81,55,60,99,
--output
0,2,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,5
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,82,8
89,90,91,92,93,94,95,96,97,98,

31
32
63
64
95

00011111
00100000
00111111
01000000
01011111

=>
=>
=>
=>
=>

000
001
001
010
010

is
is
is
is
is

the
the
the
the
the

index(=0)
index(=1)
index(=1)
index(=2)
index(=2)

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

39 of 43

96

01100000

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php
=>

## 011 is the index(=3)

#include <stdio.h>
#include <stdint.h>

#ifdef _WIN32
#include <windows.h>
void sleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void sleep(unsigned milliseconds)
{
usleep(milliseconds * 1000);
}
#endif

## // print dec in binary

void printBinary(uint16_t a)
{
// 16 bit print
for(uint16_t i = 32768; i > 0; i = (i>>1)) {
if(a & i)
printf("1");
else
printf("0");
}
printf("\n");
}
void LedFlash()
{
volatile

uint16_t a = (uint16_t)0;
while(1) {
// bit-4
a ^= (1 << 4);
printBinary(a);
sleep(1000); //1 sec delay
}

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

40 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

int main()
{
LedFlash();
return 0;
}

0000000000010000
0000000000000000
0000000000010000
0000000000000000
0000000000010000
....

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

41 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
using namespace std;

enum Suit {
CLUBS = 0,
DIAMONDS = 16,
HEARTS = 32,
};

//
//
//
//

x00
x10
x20
x30

## typedef struct Card {

enum Suit suit;
unsigned int value;
} Hand;
bool sameSuit(Suit c1, Suit c2)
{
}
bool greaterValue(unsigned int c1, unsigned int c2)
{
return ((unsigned int)(c1 & VALUE_MASK) >
}

int main()
{
Hand hand[5] = {{CLUBS, 1}, {SPADES, 5}, {DIAMONDS, 10},
cout << " same suit= " << sameSuit(hand[0].suit, hand[1
cout << " same suit= " << sameSuit(hand[0].suit, hand[4
cout << " greater value= " << greaterValue(hand[1].valu
cout << " greater value= " << greaterValue(hand[1].valu
return 0;
}

same suit= 0
same suit= 1
greater value= 0
greater value= 1

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

42 of 43

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

#include <iostream>
using namespace std;

int getSignBit(int n)
{
int sign = (n >> 31) & 0x01;;
return sign;
}

## // returns the maximum

// without using any comparison and if/else statements
int getMax(int a, int b)
{
int signa, signb;

## // a >= b : signa = 1, signb = 0;

// a < b : signa = 0, signb = 1;
signb = getSignBit(a-b);
signa = signb ^ 1;
return signa*a + signb*b;

int main()
{
int m = 215, n = 215;
cout << getMax(m,n) << endl;
return 0;
}

#include <iostream>

## int hamming(unsigned int i, unsigned int j)

{
int distance = 0;
unsigned int val = i ^ j;
while(val) {
distance++;
val &= val - 1;
}
return distance;
}

06-02-2016 06:50 PM

## C++ Tutorial: Quiz - Bit Manipulation - 2016

43 of 43

int main()
{
/* 18 = 10010
30 = 11110 */
std::cout << hamming(18, 30);
}

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php

// = 2

Custom Search

06-02-2016 06:50 PM