Query in the output of below java code

radha gogia

radha gogia

@radha-BTDzli Oct 27, 2024
public class qu11 {
    int fun(int num)
    {int count=0;
    while(num>0)
    {
        count++;
        num<<=1;
    }
       return count;
    }
    public static void main(String aa[])
    {
        qu11 obj=new qu11();
       int output= obj.fun(435);
       System.out.println(output);
    }
  
} 
According to me the output of this question must be 8 but why is its output 23..

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Oct 20, 2014

    radha gogia
    According to me the output of this question must be 8 but why is its output 23..

    can you explain how to got 8 as answer?
  • rahul69

    rahul69

    @rahul69-97fAOs Oct 26, 2014

    435=> 9 bits, shifting one bit right ,and as int is 32 bit, so keeping counter,32-9=23.