Advertisement
Svetli0o

Problem 15

Mar 15th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. /* Problem 15
  2.  * Write a program that exchanges bits 3, 4 and 5
  3.  * with bits 24, 25 and 26 of given 32-bit unsigned integer.
  4. */
  5.  
  6. using System;
  7.  
  8. class BitsExchange
  9. {
  10.     static void Main()
  11.     {
  12.         long number = uint.Parse(Console.ReadLine());
  13.         long mask = 7;
  14.         long thirdFourthFifthBits = ((mask << 3) & number) >> 3; //1)  take the 3rd, 4th and 5th bits and save them
  15.         long otherBits = ((mask << 24) & number) >> 24;          //2)  take the 24th, 25th and 26th bits and save them
  16.         number = ~(mask << 24) & number;                         //3)  make the 24th, 25th, 26th bits equal to 0
  17.         number = ~(mask << 3) & number;                          //4)  make the 3rd, 4th, 5th bits equal to 0
  18.         number = (thirdFourthFifthBits << 24) | number;          //5)  fill the 24th, 25th, 26th with the new values
  19.         number = (otherBits << 3) | number;                      //6)  fill the 3rd, 4th, 5th with the new values
  20.         Console.WriteLine(number);
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement