Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.
- namespace _15.BitsExchange
- {
- using System;
- public class BitsExchange
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.Write("Enter a whole 32-bits non-negative number: ");
- uint num = uint.Parse(Console.ReadLine());
- // First checking bit values on given positions. If founded bit values is equals, the exchange does not necessary and given number is output result.
- long bitValueOn543 = ((long)num >> 3) & 7;
- long bitValueOn262524 = ((long)num >> 24) & 7;
- uint resultNum = num;
- if (bitValueOn543 != bitValueOn262524)
- {
- // Now must nullifying bits on given positions:
- long bitMask543 = bitValueOn543 << 3;
- resultNum = (uint)(num ^ bitMask543);
- long bitMask262524 = bitValueOn262524 << 24;
- resultNum ^= (uint)bitMask262524;
- // Exchages the bits on positions 3, 4 and 5:
- long bitMaskExchange543 = bitValueOn262524 << 3;
- resultNum |= (uint)bitMaskExchange543;
- // Exchanges the bits on positions 24, 25 and 26:
- long bitMaskExchange262524 = bitValueOn543 << 24;
- resultNum |= (uint)bitMaskExchange262524;
- }
- Console.WriteLine("The Number After Bits Exchange is: {0} !", resultNum);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement