Click here to Skip to main content
15,910,009 members
Home / Discussions / C#
   

C#

 
AnswerRe: help me Pin
daniel_leto2322-Aug-11 18:33
daniel_leto2322-Aug-11 18:33 
GeneralRe: help me Pin
Not Active22-Aug-11 19:01
mentorNot Active22-Aug-11 19:01 
AnswerRe: help me PinPopular
RobCroll22-Aug-11 19:09
RobCroll22-Aug-11 19:09 
GeneralRe: help me Pin
Smithers-Jones22-Aug-11 20:58
Smithers-Jones22-Aug-11 20:58 
QuestionStackOverflowException Pin
abolfazl delbari22-Aug-11 13:08
abolfazl delbari22-Aug-11 13:08 
AnswerRe: StackOverflowException [modified] PinPopular
Luc Pattyn22-Aug-11 13:32
sitebuilderLuc Pattyn22-Aug-11 13:32 
GeneralRe: StackOverflowException Pin
abolfazl delbari22-Aug-11 14:48
abolfazl delbari22-Aug-11 14:48 
AnswerRe: StackOverflowException Pin
Luc Pattyn22-Aug-11 15:23
sitebuilderLuc Pattyn22-Aug-11 15:23 
1. the logic in your code is flawed, it isn't just too complex as I wrote earlier, it also is wrong. The easiest way to make this obvious is this: f is used once, to initialize the for-loop on "i"; modifying f later on has no net result, as f isn't used any more. This cannot possibly be correct.

I added some log statements, writing intermediate values to the console, and it clearly shows f going negative and all output being wrong. Here is what I ran:
using System;

class test {
	static void Main() {
		cycle(8);
		cycle(12);
		Console.WriteLine("done");
	}
		
	public void cycle(int f){
             Console.WriteLine("testing with f="+f);
	     int i, j;
	     if (f == 0) return;
	     for (i = f; i >= 0; i--) {
	         Console.WriteLine("i="+i);
	         for (j = 0; j <= 10; j++) {
	             //Console.WriteLine("j="+j);
	             if (i == Math.Pow(2, j)) {
	                 f = f - (int)Math.Pow(2, j);
                         // the following line replaces the whole switch construct
	                 Console.WriteLine("found bit "+j+"; f now equals "+f);
	                 break;
	             }
	         }
	     }
	     //cycle(f); // commented out to avoid the infinite loop
	}


I suggest you either learn to work with a debugger (such as Visual Studio) using single-stepping, breakpoints, watch variables, and the like. Or add log statements as I did in the above code.

2. 0x0001 is a hexadecimal literal value, and & is the bitwise AND operator. Both should be explained in all their detail in any introductory book on C#. I strongly recommend you get such a book, if you don't yet have one, and start studying the language.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

SuggestionRe: StackOverflowException Pin
lukeer22-Aug-11 20:46
lukeer22-Aug-11 20:46 
AnswerRe: StackOverflowException [modified] Pin
Luc Pattyn23-Aug-11 1:47
sitebuilderLuc Pattyn23-Aug-11 1:47 
GeneralRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 3:13
mvePIEBALDconsult23-Aug-11 3:13 
AnswerRe: StackOverflowException Pin
Luc Pattyn23-Aug-11 3:28
sitebuilderLuc Pattyn23-Aug-11 3:28 
GeneralRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 3:34
mvePIEBALDconsult23-Aug-11 3:34 
QuestionRe: StackOverflowException Pin
Luc Pattyn23-Aug-11 5:46
sitebuilderLuc Pattyn23-Aug-11 5:46 
AnswerRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 14:16
mvePIEBALDconsult23-Aug-11 14:16 
GeneralRe: StackOverflowException Pin
GParkings1-Sep-11 7:35
GParkings1-Sep-11 7:35 
GeneralRe: StackOverflowException Pin
Luc Pattyn1-Sep-11 8:11
sitebuilderLuc Pattyn1-Sep-11 8:11 
JokeRe: StackOverflowException Pin
PIEBALDconsult22-Aug-11 14:47
mvePIEBALDconsult22-Aug-11 14:47 
GeneralRe: StackOverflowException Pin
abolfazl delbari22-Aug-11 15:00
abolfazl delbari22-Aug-11 15:00 
Questionvirtual USB device Pin
Grimes22-Aug-11 11:03
Grimes22-Aug-11 11:03 
AnswerRe: virtual USB device Pin
Rutvik Dave22-Aug-11 11:26
professionalRutvik Dave22-Aug-11 11:26 
GeneralRe: virtual USB device Pin
Grimes22-Aug-11 11:41
Grimes22-Aug-11 11:41 
AnswerRe: virtual USB device Pin
Bernhard Hiller22-Aug-11 22:29
Bernhard Hiller22-Aug-11 22:29 
GeneralRe: virtual USB device Pin
Grimes22-Aug-11 22:39
Grimes22-Aug-11 22:39 
AnswerRe: virtual USB device Pin
John Clegg23-Aug-11 2:29
professionalJohn Clegg23-Aug-11 2:29 

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.