Last active
January 24, 2017 16:37
-
-
Save Hugoberry/2972d5173dce380426f0a788b8528012 to your computer and use it in GitHub Desktop.
An example of reversing bits in a 32-bit number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(){ | |
param([Int32]$b) | |
$b = (($b -band 0xf0) -shr 4) -bor (($b -band 0x0f) -shl 4) | |
$b = (($b -band 0xcc) -shr 2) -bor (($b -band 0x33) -shl 2) | |
$b = (($b -band 0xaa) -shr 1) -bor (($b -band 0x55) -shl 1) | |
$b | |
} | |
# on a 64bit machine this is an alternative to reverse() function | |
function reverse64(){ | |
param([Int32]$x) | |
$x = ($x * 0x0202020202 -band 0x010884422010) % 1023 | |
$x | |
} | |
$in = 0x04c11db7 | |
$byte0 = ($in -band 0xff) | |
$byte1 = ($in -band 0xff00) -shr 8 | |
$byte2 = ($in -band 0xff0000) -shr 16 | |
$byte3 = ($in -band 0xff000000) -shr 24 | |
$out = ((reverse $byte0) -shl 24 ) -bor ((reverse $byte1) -shl 16 ) -bor ((reverse $byte2) -shl 8 ) -bor (reverse $byte3 ) | |
"{0:X0}" -f $out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment