Click here to Skip to main content
15,912,283 members
Home / Discussions / C#
   

C#

 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus314-Jan-08 23:29
invictus314-Jan-08 23:29 
GeneralRe: Count bitpattern occurences in textstrings Pin
Russell Jones15-Jan-08 0:02
Russell Jones15-Jan-08 0:02 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 0:05
invictus315-Jan-08 0:05 
GeneralRe: Count bitpattern occurences in textstrings Pin
Russell Jones15-Jan-08 0:45
Russell Jones15-Jan-08 0:45 
GeneralRe: Count bitpattern occurences in textstrings [modified] Pin
Luc Pattyn15-Jan-08 0:56
sitebuilderLuc Pattyn15-Jan-08 0:56 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 23:20
invictus315-Jan-08 23:20 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn16-Jan-08 8:56
sitebuilderLuc Pattyn16-Jan-08 8:56 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 8:41
invictus319-Jan-08 8:41 
I started reading up on bit-operators in C# and came up with this solution:

static int[] countbits(string s)<br />
{<br />
	int[] vector = { 0, 0, 0, 0 };<br />
<br />
	for (int i = 0; i < s.Length; i++)<br />
	{<br />
		for (int j = 6; j >= 0; j--)<br />
			vector[((s[i] >> j) & 0x03)]++;   //masker med 0x03 for å kun få de 2 siste bitsene<br />
		if (i < s.Length - 1)<br />
			vector[(((s[i] << 1) | (s[i + 1] >> 7)) & 0x03)]++;<br />
	}<br />
<br />
	return vector;<br />
}


Not sure if its the best way, but it works! Smile | :)

Any suggestions for improvement are welcome. Have optimized it some, but I wouldnt be surprised if there are better ways to solve this :p

Mvh Sverre
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 9:13
sitebuilderLuc Pattyn19-Jan-08 9:13 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 11:16
invictus319-Jan-08 11:16 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 11:36
sitebuilderLuc Pattyn19-Jan-08 11:36 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 12:57
invictus319-Jan-08 12:57 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 14:01
sitebuilderLuc Pattyn19-Jan-08 14:01 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 0:09
invictus321-Jan-08 0:09 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn21-Jan-08 0:45
sitebuilderLuc Pattyn21-Jan-08 0:45 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 0:57
invictus321-Jan-08 0:57 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn21-Jan-08 1:09
sitebuilderLuc Pattyn21-Jan-08 1:09 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 22:41
invictus321-Jan-08 22:41 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn22-Jan-08 1:07
sitebuilderLuc Pattyn22-Jan-08 1:07 
GeneralRe: Count bitpattern occurences in textstrings Pin
Ennis Ray Lynch, Jr.15-Jan-08 11:00
Ennis Ray Lynch, Jr.15-Jan-08 11:00 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 23:10
invictus315-Jan-08 23:10 
GeneralRe: Count bitpattern occurences in textstrings Pin
Ennis Ray Lynch, Jr.16-Jan-08 4:11
Ennis Ray Lynch, Jr.16-Jan-08 4:11 
GeneralRe: Count bitpattern occurences in textstrings Pin
PIEBALDconsult15-Jan-08 15:41
mvePIEBALDconsult15-Jan-08 15:41 
GeneralCatching MouseMove Event Pin
stancrm14-Jan-08 21:16
stancrm14-Jan-08 21:16 
GeneralRe: Catching MouseMove Event Pin
Nouman Bhatti14-Jan-08 22:26
Nouman Bhatti14-Jan-08 22:26 

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.