'commit'
This commit is contained in:
15
C++课程/第十四课位运算训练精讲/p164.cpp
Normal file
15
C++课程/第十四课位运算训练精讲/p164.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int a, b, c, d;
|
||||
cin >> a >> b;
|
||||
do {
|
||||
c = a ^ b;
|
||||
d = (a & b) << 1;
|
||||
a = c;
|
||||
b = d;
|
||||
} while (b != 0);
|
||||
cout << a;
|
||||
return 0;
|
||||
}
|
||||
11
C++课程/第十四课位运算训练精讲/p164_2.cpp
Normal file
11
C++课程/第十四课位运算训练精讲/p164_2.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n = 0, x;
|
||||
cin >> x;
|
||||
for (n = 0; x != 0; x &= x - 1)
|
||||
n++;
|
||||
cout << n;
|
||||
return 0;
|
||||
}
|
||||
14
C++课程/第十四课位运算训练精讲/p164_3.cpp
Normal file
14
C++课程/第十四课位运算训练精讲/p164_3.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n = 4095;
|
||||
int count = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
count++;
|
||||
n >>= 1;
|
||||
}
|
||||
cout << count;
|
||||
return 0;
|
||||
}
|
||||
20
C++课程/第十四课位运算训练精讲/p165.cpp
Normal file
20
C++课程/第十四课位运算训练精讲/p165.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int a,b,c,d,e,f;
|
||||
cin>>a>>b;
|
||||
c=a&b;
|
||||
d=a^b;
|
||||
while(c)
|
||||
{
|
||||
e=d;
|
||||
f=c<<1;
|
||||
c=e&f;
|
||||
d=e^f;
|
||||
}
|
||||
cout<<d;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
19
C++课程/第十四课位运算训练精讲/x&(x-1).cpp
Normal file
19
C++课程/第十四课位运算训练精讲/x&(x-1).cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include<bits/stdc++.h>
|
||||
//https://blog.csdn.net/sandwichsauce/article/details/79847953
|
||||
using namespace std;
|
||||
int func(int x)
|
||||
{
|
||||
int count = 0;
|
||||
while (x)
|
||||
{
|
||||
count++;
|
||||
x = x&(x - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cout << func(2048) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
C++课程/第十四课位运算训练精讲/~$循环结构中应用位运算.pptx
Normal file
BIN
C++课程/第十四课位运算训练精讲/~$循环结构中应用位运算.pptx
Normal file
Binary file not shown.
BIN
C++课程/第十四课位运算训练精讲/位运算训练精讲.pptx
Normal file
BIN
C++课程/第十四课位运算训练精讲/位运算训练精讲.pptx
Normal file
Binary file not shown.
12
C++课程/第十四课位运算训练精讲/异或交换两个数.cpp
Normal file
12
C++课程/第十四课位运算训练精讲/异或交换两个数.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
int a=12,b=34,temp;
|
||||
printf("Original result: a=%d,b=%d\n",a,b);
|
||||
temp=a^b;
|
||||
a=temp^a;
|
||||
b=temp^b;
|
||||
printf("Transformed result: a=%d,b=%d\n",a,b);
|
||||
return 0;
|
||||
}
|
||||
12
C++课程/第十四课位运算训练精讲/异或交换两个数_加法.cpp
Normal file
12
C++课程/第十四课位运算训练精讲/异或交换两个数_加法.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 5, b = 5;
|
||||
printf("a=%d b=%d\n", a, b);
|
||||
a = a ^ b;
|
||||
b = a ^ b;
|
||||
a = a ^ b;
|
||||
printf("a=%d b=%d\n", a, b);
|
||||
}
|
||||
29
C++课程/第十四课位运算训练精讲/快速幂.cpp
Normal file
29
C++课程/第十四课位运算训练精讲/快速幂.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include<iostream>
|
||||
#include <bitset>
|
||||
using namespace std;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void printEr(int b) {
|
||||
cout<<bitset<sizeof(b)*2>(b)<<endl;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int poww(int a, int b) {
|
||||
int ans = 1, base = a;
|
||||
while (b >0) {
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>b
|
||||
printEr(b);
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݵĺ<DDB5><C4BA><EFBFBD><EFBFBD>㷨
|
||||
if (b & 1) //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD> & 1 <20>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>ĩλ<C4A9><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||||
ans *= base;
|
||||
base *= base;
|
||||
//<2F><><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>2
|
||||
b >>= 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout << poww(2,5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
C++课程/第十四课位运算训练精讲/快速幂_2.cpp
Normal file
25
C++课程/第十四课位运算训练精讲/快速幂_2.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int quick(int a,int b)
|
||||
{
|
||||
int s = 1;
|
||||
while (b > 0) {
|
||||
if (b & 1) {//b=b>>1<><31>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>϶<EFBFBD><CFB6><EFBFBD>ִ<EFBFBD>и<EFBFBD>if<69>ж<EFBFBD>
|
||||
s = s * a;
|
||||
}
|
||||
a = a * a;
|
||||
cout<<"b="<<b<<endl;
|
||||
cout<<"s="<<s<<endl;
|
||||
cout<<endl;
|
||||
b = b >> 1;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%d",quick(2,10));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user