Click here to Skip to main content
15,891,529 members
Home / Discussions / C#
   

C#

 
AnswerRe: Looking for an alternative to bit shifting Pin
PIEBALDconsult1-Aug-08 6:34
mvePIEBALDconsult1-Aug-08 6:34 
AnswerRe: Looking for an alternative to bit shifting Pin
Guffa1-Aug-08 11:01
Guffa1-Aug-08 11:01 
GeneralRe: Looking for an alternative to bit shifting Pin
PIEBALDconsult1-Aug-08 15:17
mvePIEBALDconsult1-Aug-08 15:17 
GeneralRe: Looking for an alternative to bit shifting Pin
Guffa2-Aug-08 1:48
Guffa2-Aug-08 1:48 
GeneralRe: Looking for an alternative to bit shifting [modified] Pin
PIEBALDconsult2-Aug-08 5:41
mvePIEBALDconsult2-Aug-08 5:41 
GeneralRe: Looking for an alternative to bit shifting Pin
Guffa2-Aug-08 10:13
Guffa2-Aug-08 10:13 
AnswerRe: Looking for an alternative to bit shifting Pin
PIEBALDconsult2-Aug-08 8:18
mvePIEBALDconsult2-Aug-08 8:18 
AnswerRe: Looking for an alternative to bit shifting [modified] Pin
PIEBALDconsult2-Aug-08 8:52
mvePIEBALDconsult2-Aug-08 8:52 
Ha ha ha! Here it is in C#

private static readonly int[] masks = unchecked ( new int[] { (int) 0xFFFFFFFF , 0x00FFFFFF , 0x0000FFFF , 0x000000FF } ) ;
 
public static unsafe int
ShiftAndSumArray
(
    int[] Array
,
    uint  Shift
)
{
    int result = 0 ;
           
    fixed ( int* b = Array )
    {
        int* a = (int*) ( ((byte*) b) + ( Shift %= sizeof(int) ) ) ;
        int  i ;
                 
        for ( i = 0 ; i < Array.Length-1 ; i++ )
        {
            result += a [ i ] ;
        }
 
        result += a [ i ] & masks [ Shift ] ;
    }
             
    return ( result ) ;
}


This appears to take about half the time of shifting each int separately, but it doesn't involve very much time either way;
100,000,000 (10,000 passes across an array of 10,000 ints) this way takes 507 milliseconds, the shifting technique takes 969 milliseconds.
(And 2665 milliseconds for the division technique.)

public static int
ShiftAndSumArray2
(
    int[] Array
,
    uint  Shift
)
{
    int result = 0 ;
 
    int shift = (int) Shift % sizeof(int) * 8 ;

    // Using foreach is not much slower 
    for ( int i = 0 ; i < Array.Length ; i++ )
    {
        result += Array [ i ] >> shift ;
    }            
 
    return ( result ) ;
}



Correction: Earlier I had forgotten to Reset the Stopwatch D'Oh! | :doh: , corrected results

457 milliseconds for altered pointer
459 milliseconds for shifted values
1768 milliseconds for division

I have also quickened up the implementations somewhat.
Questionlocalizable form gridview issue Pin
muharrem1-Aug-08 4:37
muharrem1-Aug-08 4:37 
AnswerRe: localizable form gridview issue Pin
nelsonpaixao1-Aug-08 13:17
nelsonpaixao1-Aug-08 13:17 
GeneralRe: localizable form gridview issue Pin
muharrem2-Aug-08 1:51
muharrem2-Aug-08 1:51 
QuestionService to show form Pin
BenGriffiths1-Aug-08 4:17
BenGriffiths1-Aug-08 4:17 
AnswerRe: Service to show form Pin
paas1-Aug-08 4:43
paas1-Aug-08 4:43 
AnswerRe: Service to show form Pin
vikas amin1-Aug-08 4:53
vikas amin1-Aug-08 4:53 
QuestionMessage Removed Pin
1-Aug-08 3:50
tkrn1-Aug-08 3:50 
AnswerRe: Dataset Designer using it programmatically? Pin
nelsonpaixao1-Aug-08 14:06
nelsonpaixao1-Aug-08 14:06 
QuestionTemplates Pin
Dirso1-Aug-08 2:45
Dirso1-Aug-08 2:45 
AnswerRe: Templates Pin
J4amieC1-Aug-08 2:48
J4amieC1-Aug-08 2:48 
QuestionProgrammatically Added SSIS Package Pin
Jammer1-Aug-08 1:29
Jammer1-Aug-08 1:29 
AnswerRe: Programmatically Added SSIS Package Pin
Jammer1-Aug-08 6:12
Jammer1-Aug-08 6:12 
QuestionSharePoint Problem from client pc Pin
manju#1231-Aug-08 1:04
manju#1231-Aug-08 1:04 
QuestionGet the code of an Stored procedure Pin
anderslundsgard1-Aug-08 0:42
anderslundsgard1-Aug-08 0:42 
AnswerRe: Get the code of an Stored procedure Pin
Rob Philpott1-Aug-08 0:45
Rob Philpott1-Aug-08 0:45 
GeneralRe: Get the code of an Stored procedure Pin
Paul Conrad1-Aug-08 5:39
professionalPaul Conrad1-Aug-08 5:39 
AnswerRe: Get the code of an Stored procedure Pin
Anurag Gandhi1-Aug-08 1:00
professionalAnurag Gandhi1-Aug-08 1:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.