IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Bitwise Operators - Discussion

@ : Home > C Programming > Bitwise Operators > Point Out Correct Statements - Discussion

1. 

Which of the following statements are correct about the program?

#include<stdio.h>

int main()
{
    unsigned int num;
    int i;
    scanf("%u", &num);
    for(i=0; i<16; i++)
    {
        printf("%d", (num<<i & 1<<15)?1:0);
    }
    return 0;
}

[A]. It prints all even bits from num
[B]. It prints all odd bits from num
[C]. It prints binary equivalent num
[D]. Error

Answer: Option B

Explanation:

If we give input 4, it will print 00000000 00000100 ;

If we give input 3, it will print 00000000 00000011 ;

If we give input 511, it will print 00000001 11111111 ;


Viraj said: (Sun, Nov 21, 2010 04:50:54 AM)    
 
1 <<15 = -32768 i.e. 1000 0000 0000 0000

In the loop the bits of 'num' are brought to the MSB one by one using left shift and ANDed with the 1000 0000 0000 0000

Mohanty said: (Sat, Aug 6, 2011 01:00:32 PM)    
 
What is "i<16"?

Abhishek said: (Mon, Sep 19, 2011 07:35:13 PM)    
 
@Mohanty
num<<i
shifts one bit, so to shift 16 bits the condition is set as i<16

Ankit said: (Mon, Dec 26, 2011 11:31:54 PM)    
 
Please explain it clearly.

Sadhna said: (Sat, Jan 21, 2012 09:18:26 AM)    
 
Hi, I have a doubt in printf statement it will print either 0 or 1 as we are printing the result of a conditional statement, can someone explain this to me ?

Prem said: (Mon, Jan 30, 2012 10:06:17 AM)    
 
If the condition is true it will print as 1 otherwise 0.

Bhakar said: (Mon, Feb 6, 2012 03:52:33 PM)    
 
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}

Lets assume num = 4.

Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
same repeat

Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
AT 14 LOOP POSITION

Then num<<14 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
0000 0000 0000 0100

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!