结合按位和补语运算符

时间:2016-07-03 07:43:07

标签: c bitwise-operators

在下面的代码中,由于x的按位补码为-1(使用2' s补码)且y为2,我期望z的值为零,但我得到z的值为2当我运行程序时。任何人都可以解释一下我错在哪里?

CODE

void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
        {
            List<string> resultsoftextfound = new List<string>();
            List<string> filePathList = new List<string>();
            List<string> restrictedFiles = new List<string>();
            int numberoffiles = 0;
            int numberofdirs = 0;
            int countTextToSearch = 0;

            try
            {
                filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null).ToList();
            }
            catch (Exception err)
            {
                string ad = err.ToString();
            }

            foreach (string file in filePathList)
            {
                try
                {
                    _busy.WaitOne();
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        return;
                    }

                    for (int i = 0; i < textToSearch.Length; i++)
                    {
                        List<MyProgress> prog = new List<MyProgress>();
                        if (File.ReadAllText(file).Contains(textToSearch[i].ToLower()))
                        {
                            resultsoftextfound.Add(file + "  " + textToSearch[i]);
                            numberoffiles++;
                            prog.Add(new MyProgress { Report1 = file, Report2 = numberoffiles.ToString() });
                            backgroundWorker1.ReportProgress(0, prog);
                        }
                    }   
                    numberofdirs++;
                    label1.Invoke((MethodInvoker)delegate
                    {
                        label1.Text = numberofdirs.ToString();
                        label1.Visible = true;
                    });
                }
                catch (Exception)
                {
                    restrictedFiles.Add(file);
                    continue;
                }
            }
        }

1 个答案:

答案 0 :(得分:4)

0的按位赞美是1所有AND所以AND使用它可以准确地为2提供其他输入,在这种情况下0 = 00000000 ~0 = 11111111 2 = 00000010 ~0&2 = 00000010 }。

假设8位节省空间:

{{1}}