以下代码的含义是什么? [英] What is meaning of following code?

查看:98
本文介绍了以下代码的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 int printSubset(char * set,int& elements)
{
int total_subsets = pow(2,elements);
cout<<\ n总子集= 2 ^<<元素<<=<< total_subsets<< endl;
cout<<\ nEmpty set; (b c = 0; c< total_subsets; c ++)
$
for(int j = 0; j< elements; j ++)
{
if(c&(1<< j))
{
cout<<设置[J];
}
}
cout<<\ n\ n;
}
}





我的尝试:



实际上这个代码用于制作一组的子集

但我不明白以下代码是如何工作的





特别是

条件所以请告诉我有关情况的信息。

 for (int c = 0; c< total_subsets; c ++)
{
for(int j = 0; j< elements; j ++)
{
if(c&(1<< j))
{
cout<<设置[J];
}
}
cout<<\ n\ n;
}

解决方案

当您不理解一些代码时,请查看并剖析它如果(c&(1<< j))

分解为

 如果(...)

并且

 c&(1<< j)

第一部分我知道你理解 - 如果这是正常的 ,第二部分是评估为真或假的条件。

 c& ( 1 << j)

;也分为两部分:

 c& ... 

  1 << j 

第一部分是两个值的二进制AND:c和第二部分,二进制AND很简单:对于输入中的每个位,当且仅当两者都有时,结果位为1输入位是1.否则它是0.(有关C中的按位运算符的有趣事实 - GeeksforGeeks [< a href =https://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/\"target =_ blanktitle =新窗口> ^]应该有帮助)

第二部分也很简单:它是向右移动的1个位置(上面的链接也解释了这一点)。所以如果j为0则结果为1,对于j == 1,结果为2,对于j = 2,它给出4,依此类推。



所以整个if检查 c中特定位置的1位



这些都在文档中: C / C ++中的运算符 - GeeksforGeeks [ ^ ]


int printSubset(char *set,int &elements)
{
	int total_subsets = pow(2,elements);
	cout<<"\n Total subset are = 2^"<< elements <<" = "<< total_subsets <<endl;
	cout<<"\nEmpty set";
    
    for(int c=0; c<total_subsets; c++)
    {
      for(int j=0; j<elements; j++)
       {
		if(c&(1<<j))
          {
          	 cout<< set[j];
          }          
	  }
	  cout<<"\n\n";
    }
}



What I have tried:

Actually this code is for making subsets of a set
But i dont understand the following code that how does it works


Especially the
if condition so please tell me about the condition.

for(int c=0; c<total_subsets; c++)
{
  for(int j=0; j<elements; j++)
   {
if(c&(1<<j))
      {
         cout<< set[j];
      }
   }
  cout<<"\n\n";
}

解决方案

When you don't understand a bit of code, look at it and dissect it into it's component parts.

if(c&(1<<j))

Breaks down into

if (...)

and

c&(1<<j)

The first part I know you understand - it's a normal if, and the second part is the condition which evaluates to true or false.

c & (1 << j)

;Is also in two parts:

c & ...

And

1 << j

The first part is a binary AND of the two values: c and the second part, and a binary AND is simple: for each bit in the inputs, the resulting bit is 1 if and only if both the input bits are 1. Otherwise it's a 0. (Interesting Facts about Bitwise Operators in C - GeeksforGeeks[<a href="https://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/" target="_blank" title="New Window>^] should help)
The second part is also simple: it's a 1 shifted j places to the right (the link above also explains this). So if j is 0 then it results in 1, for j == 1 it results in 2, for j = 2 it gives 4, and so on.

So the whole if is checking for a 1 bit in a specific position in c

This is all in the documentation: Operators in C / C++ - GeeksforGeeks[^]


这篇关于以下代码的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆