Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to write if (n & 1) in C#

1 Answer

0 votes
using System;
   
class Program
{
    static void print_bits(int n) { 
        for (int i = 7; i >= 0; i--)
            Console.Write((n >> i) & 1);
        Console.WriteLine();
    } 
  
    static void Main()
    {
        int n = 178;
        
        print_bits(n);
        print_bits(n & 1);
 
        if ((n & 1) == 1) 
            Console.Write("Yes\n"); 
        else
            Console.Write("No\n"); 
             
        n = 179;
        
        print_bits(n);
        print_bits(n & 1);
        
        if ((n & 1) == 1) 
            Console.Write("Yes\n"); 
        else
            Console.Write("No\n"); 
    }
}
   
   
/*
run:
   
10110010
00000000
No
10110011
00000001
Yes
   
*/

 



answered Mar 11, 2019 by avibootz
edited Mar 12, 2019 by avibootz

Related questions

1 answer 143 views
143 views asked Mar 11, 2019 by avibootz
2 answers 165 views
165 views asked Feb 14, 2017 by avibootz
10 answers 469 views
469 views asked Feb 18, 2015 by avibootz
1 answer 78 views
...