编码2的幂的最有效方法是通过整数的位移.
1 << n 给我 2^n
但是,如果我的数字大于inta或a中允许的最大值long,我可以用什么来有效地操纵2的幂?
(我需要能够对数字执行加法,乘法,除法和模数运算)
我想做一些非常简单的事情:我的函数有字符串参数,我想将它链接到一些常量字符串,然后将结果输出到控制台,如下所示:
void test(string s){
cout << "Parameter of this function was: " << s;
}
Run Code Online (Sandbox Code Playgroud)
在其他语言中,这样的链接有效,但在C++中,编译器不满意: error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
我必须为家庭作业编写这段代码,但我甚至不知道从哪里开始.这是我必须编写的方法的javadoc.
/**
* Sets a 4-bit nibble in an int
* Ints are made of eight bytes, numbered like so: 7777 6666 5555 4444 3333 2222 1111 0000
*
* For a graphical representation of this:
* 1 1 1 1 1 1
* 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Nibble3|Nibble2|Nibble1|Nibble0|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Examples:
* setNibble(0xAAA5, 0x1, 0) //=> 0xAAA1
* setNibble(0x56B2, 0xF, 3) //=> …Run Code Online (Sandbox Code Playgroud) 我正在写一段代码,我必须执行2的除法.以下代码行给出了正确的输出
ans = ans + ((long long)cnt * (cnt-1))/2;
Run Code Online (Sandbox Code Playgroud)
但是,当我改变它
ans = ans + ((long long)cnt * (cnt-1)) >> 1;
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么问题
在我的设置中,值永远不会消极
这是代码
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int s[1000000];
int main(){_
int t;
cin>>t;
while(t--){
int n,i,z,sum=0,p,cnt=0;
unsigned long long int ans=0;
cin>>n;
for(i=0;i<n;i++){
cin>>z;
sum+=z;
s[i]=sum;
}
sort(s,s+n);
i=0;
while(i<n){
p=s[i];
cnt=0;
while(s[i]==p){
cnt++;
i++;
}
ans=ans+((unsigned long long)cnt*(cnt-1))>>1;
}
cnt=0;
for(int i=0;i<n && s[i]<=0;i++){
if(s[i]==0){
cnt++;
}
}
ans+=cnt;
cout<<ans<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入1 …
我必须在一个变量中存储多达7个字节的数据,并能够读取和写入各个位.有4个字节,这是小菜一碟,我只是使用for循环并执行一次移位来写入该位或读取它:
data : int64;
data := $01 + ($00 shl 8 ) + ($00 shl 16 ) + ($FF shl 24);
for i := 31 downto 0 do
begin
if ((data shr i) and 1) = 1 then ShowMessage('data bit was one')
else ShowMessage('data Bit was Zero');
end;
Run Code Online (Sandbox Code Playgroud)
这将以正确的顺序读出位.
但是,当我尝试使用此方法超过32位似乎倒下时:
data : int64;
data := $01 + ($00 shl 8 ) + ($00 shl 16 ) + ($00 shl 24) + ($FF shl 32);
for i := 39 downto 0 …Run Code Online (Sandbox Code Playgroud) 在C我有这样的enum:
enum {
STAR_NONE = 1 << 0, // 1
STAR_ONE = 1 << 1, // 2
STAR_TWO = 1 << 2, // 4
STAR_THREE = 1 << 3 // 8
};
Run Code Online (Sandbox Code Playgroud)
为什么1 << 3等于8而不是6?
我正在C语言中工作,并假设我有2个字节的小字节序:
buffer[0] = 0x01; buffer[1] = 0x02;
Run Code Online (Sandbox Code Playgroud)
如何将以上内容转换为组合的12位数字?因此,合并后应如下所示:
0x0201
Run Code Online (Sandbox Code Playgroud) 我必须在一个字节中rearagne位.我解决了这个问题:
uint8_t c;
uint8_t string[3];
string1[2] = (((c&(1<<0))!=0)<<6)|
(((c&(1<<1))!=0)<<1)|
(((c&(1<<2))!=0)<<0)|
(((c&(1<<3))!=0)<<2)|
(((c&(1<<4))!=0)<<3)|
(((c&(1<<5))!=0)<<4)|
(((c&(1<<6))!=0)<<5)|
(((c&(1<<7))!=0)<<7);
Run Code Online (Sandbox Code Playgroud)
basicly:
如果bit0为1,则向左移动1 6次.
如果bit1为1,则向左移动1 0次.....
有更好的解决方案吗?
我对C很新,我试图理解CI中的按位运算符在我面前找到这个代码(将2转换为37)
int main(void)
{
int x = 2;
x = (x<<x<<x) | (x<<x<<x) | (x << !!x) | !!x ;
printf("%d\n" , x ); // prints 37
}
Run Code Online (Sandbox Code Playgroud)
现在这是我第一次看到这样的东西 (x<<x<<x),我不明白它在做什么.任何人都可以详细解释代码中的第二行吗?
如何将一个uint16_t转换为两部分?
uint16_t value = 0x7133;
uint8_t partA = (uint8_t)((value & 0xFF00) >> 8);
uint8_t partB = (uint8_t)(value & 0x00FF);
std::cout << std::hex << partA << std::endl;
std::cout << std::hex << partB << std::endl;
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我用partAas q和partBas 3代替0x71and 0x33。