-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitshiftsigned.cpp
More file actions
38 lines (34 loc) · 889 Bytes
/
bitshiftsigned.cpp
File metadata and controls
38 lines (34 loc) · 889 Bytes
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
38
#include <cinttypes>
#include <iostream>
using namespace std;
template <class T>
bool testBit(T target, int bit)
{
T mask = (1 << bit);
return target & mask;
}
template <class T>
void printBits(T target)
{
for(int bit = 8 * sizeof(T) -1; bit >=0 ; bit--) {
cout << testBit(target,bit);
}
}
int main(int argc, char ** argv, char ** env)
{
for(int bit = 0; bit < 8; bit++ ) {
unsigned char target = 0x80;
target >>= bit;
cout <<"right shift unsigned 0x80 " << bit << " bits right =" ;
printBits(target);
cout << " target=" << (unsigned int)(target & 0xff) << "\n";
}
cout << "\n";
for(int bit = 0; bit < 8; bit++ ) {
char target = 0x80;
target >>= bit;
cout <<"right shift signed 0x80 " << bit << " bits right =" ;
printBits(target);
cout << " target=" << (int)(target) << "\n";
}
}