 SAR, SHL, SHR

cCalc v. 1.06
Sevag Krikorian

Licence:
Freeware.  Use it as you will, at your own risk.
Package it with anything, anywhere.

Kudos and Curses to:
kahlinor@yahoo.com

Contents:
	Updates		section 1
	Using cCalc		section 2
	Keyboard shortcuts	section 3
	Bit Formulas		section 4
	
	
[section 1]
Updates:
1.05	-fixed error in display of sign flag
1.06	-pressing tab selects the existing text in edit box
	-fixed eflags not updating properly during some actions
	-added SAR instruction

[section 2]
Use:

For beginners:
	Help one understand how bits and flags are modified
when using cpu instructions supported in cCalc.

Advanced users:
	Useful for generating bit-masks and testing out new
bit manipulation formulas.

Supported instructions:
SHL	(same as SAL)
SHR
SAR
ROR
ROL
NOT
NEG
AND
OR
XOR
ADD
SUB
DIV
MUL
TEST
CMP	(cmp input,op)

Note: it is up to the user to know which flags are defined
and which are not defined with each instruction.

Input:
Menu-selectable, hexadecimal or decimal

Arithmatic notes:
Input is considered the accumulator (EAX)
Op is considered the operand (for DIV, MUL)
and source (for ADD, SUB, CMP, TEST)

MUL & DIV - always 32-bit, EBX is zero-extended.

[section 3]

Keyboard shortcuts:

	Command buttons have keyboard shortcuts.
Shortcuts are accessed by pressing ctrl-key, usually
denoted by an underlined letter in the command name.
For the ones that don't have underlined letters:

ESC		quit program
Copy To Op	ctrl enter
ROL		ctrl left
ROR		ctrl right
SAR		ctrl down
Clear All	ctrl delete
Set All	ctrl insert
TEST		ctrl z



[section 4]
Format for section
Formula	- an equasion
Use		- the function and uses of the formula
Result	- effect on EFLAGS
See It	- see it in action using cCalc
		  each line represents a command to input
		  Unless otherwise noted, consider:
		  Input field as dest operand or accumulator (EAX)
		  Op field as source operand

Dual formulas
	The formulas presented here may be reversed to get
	the opposite results.  Reversing involves exchanging
	the operands:
	
	x-1	<=>	x+1
	-x	<=>	!(x+1)
	&	<=>	|
	x	<=> 	x
	!x	<=> 	!x
	
	--------------------------------------------------
	Formula: x & (x-1)
	
	Use: 	determine if unsigned x is power of 2
		clear right-most set bit
			
	Result:	zero set = true
		
	See it:
		Input		32
		Op		31
		AND		= zero set
		
		Input		31
		Op		30
		AND		= zero clear
	
	--------------------------------------------------
	Formula: x & (x+1)
	
	Use:	determine if unsigned x is x^n-1
	
	Result: zero set = true
	
	See it:
		Input		31
		OP		32
		AND		= zero set
		
		Input		32
		Op		33
		AND		= zero clear
		
	--------------------------------------------------
	Formula: x & (-x)
	
	Use:	isolate the rightmost set bit
			(find the smallest 1 bit)

	Result: zero set = no set bits, x = 0
	
	See it:
		Input		180
		Copy To Op
		NEG
		AND		= 4
		
	--------------------------------------------------
	Formula -x & (x+1)
	
	Use:	isolate the rightmost 0 bit
	
	Result:	zero set = no set bits, x = 0
	
	See it:
		Input		55
		Op		56
		NEG
		AND		= 8
		
	--------------------------------------------------
	Formula: 	a) !x & (x-1)
			b) ! (x | -x)
			c) (x & -x) -1
			 
	Use:	Create mask of trailing zeros
		eg: 0100_1000  =>  0000_0111
			
	Result: zero set = x is 1
				
	See it:
		a)	Input		1000
			Op		999
			NOT
			AND		= 7
			
		b)	Input		1000
			Copy To Op
			NEG
			OR
			NOT		= 7
			
		c)	Input		1000
			Copy To Op
			AND
			Op		1
			SUB		= 7
			
	--------------------------------------------------
	Formula: x ^| (x-1)
	
	Use:	Create a mask of rightmost set bit
			and trailing zeros
			
	See it:
		Input		168
		Op		167
		XOR		= 15
		
	--------------------------------------------------
	Formula: x | (x-1)
	
	Use:	Right extend rightmost set bit
	
	See It:
		Input		168
		Op		167
		OR		= 175
		
	--------------------------------------------------
	Formula: ((x | (x-1)) +1) & x
	
	Use:	Isolate leftmost set bit
			determine if unsigned x is in the form
			2^j - 2^k where j => k => 0
			
	Result:	zero set = true
	
	See it:
		Input		3
		Op		2
		OR
		Op		1
		ADD
		Op		3
		AND		= 0  zero set, true
	
	--------------------------------------------------
	
fin
