Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- class ChangeBitsInteger
- {
- static void Main()
- {
- //Must change places between 3rd, 4th, 5th and 24th,25th,26th bit in integer number
- var timer = System.Diagnostics.Stopwatch.StartNew();
- Console.Write("Enter a number and see the result of\n changing 3,4,5bit with 24,25,26bit: ");
- int num = int.Parse(Console.ReadLine());
- //Bits 3-24, 4-25, 5-26
- //int num Example = 1234567890; //1001'001'100101100000001011'010'010
- int mask3 = 7 << 3; //7 in Binary is 111, 3 positions left to check 3,4,5bit
- int mask3bits = mask3 & num;
- //Bits 3-0; 4-1; 5-0
- int result3 = mask3bits >> 3; //010
- int mask24 = 7 << 24; //Check 24,25,26 bit
- int mask24bits = mask24 & num;
- //Bits 24-1, 25-0, 26-0
- int result24 = mask24bits >> 24; //001
- //Move 24,25,26 bits to 3,4,5 position
- int replacebits3to24 = result3 << 24; //00'010'000000000000
- //Move 3,4,5 bits to 24,25,26 position
- int replacebits24to3 = result24 << 3; //00000000000'001'000
- int resultAll = replacebits24to3 | replacebits3to24; //00'010'0000'001'000
- int oppositemask3 = ~mask3; //Opposite mask 11111111111000111
- int oppositemask24 = ~mask24; //Opposite mask 11100011111111111
- int oppositemaskAll = oppositemask24 & oppositemask3; //General opposite mask 111'000'11111111111111'000'111
- int nummask = oppositemaskAll & num; //Take values from num to General mask 101'000'01000111010001'000'101
- int finalnumber = nummask | resultAll; //3,4,5bit <=> 24,25,26 bit
- Console.WriteLine("Number \"{0}\", gets: \"{1}\"", num, finalnumber);
- var elapsed = timer.ElapsedMilliseconds;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement