Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: Dimitar Stefanov en 18 de Julio 2016, 20:55
-
Ejercicio:
Using bitwise operators, write a program that uses an expression to find the value of the bit at index 3 of an unsigned integer read from the console.
The bits are counted from right to left, starting from bit 0.
The result of the expression should be either 1 or 0. Print it on the console.
El código:
using System;
class Program {
static void Main() {
//Console.WriteLine(Convert.ToString(35,2));
//Console.WriteLine(Convert.ToString(~35,2));
int num;
Console.Write("Introduce un número entero: ");
num = Convert.ToInt16(Console.ReadLine());
//Console.WriteLine(Convert.ToString(num, 2));
int mask = 1 << 3;
int nAnMask = num & mask;
int bit = nAnMask >> 3;
Console.WriteLine(bit);
}
}
Saludos.