/* Am not able to understand the operation happening in function showbits(), please help*/

#include <stdio.h>
void showbits (unsigned char);

int main()
   unsigned char num=225,j,k;
   printf(\nDecimal %d is same as binary", num);
   showbits (num);

   for(j=0; j<=4; j++)
       printf (\n%d left shift %d gives", num, j);
    return 0;

 void showbits (unsigned char n)
   int i;
   unsigned char j,k, andmask;

   for(i=7; i>=o; i--)
     andmask = 1 << j;
     k = n & andmask;       /* what happens during this instruction */
     k == 0 ? printf("0") : printf("!");       /*and here too, pls help.*/

1 Year
Discussion Span
Last Post by surfingturtle
Featured Replies
  • 1
    vegaseat 1,720   1 Year Ago

    You have quite a few errors in your code. Line 16 needs a " Line 27 replace o with 0 Line 32 replace ! with 1 To see what's going on use a few temporary test prints. Read More


k = n & andmask;

The bitwise AND will set each corresponding bit to 1 if the same bit in both arguments is 1. Otherwise the bit will be set to 0. Since andmask only has at most one bit set to 1, the purpose of this statement is to check the bit at position j to see if it's set.

k will be zero if both bits are not set and nonzero if both bits are set.

k == 0 ? printf("0") : printf("!");

And this statement prints the result of whether the bit is set or not. It's equivalent to the following:

if (k == 0)

Thankyou for your prompt response

My doubt persists, am getting still stuck in....... k = n & andmask; instruction.

Please try getting my way of understanding..from instruction on number 30

j is 7, left shifting 1 results in 10000000 which promptly gets stored in andmask, now this is anded with n which is 225 ( I presume 225 is stored in binary form in n as 11100001 after anding with andmask we get 10000000 which gets promptly stored in k, now when k has these bits how does the next instruction manages to perform, its not just 1 bit but many.....


The second time in the loop, j will be 6, so left shifting 1 results in 01000000, wich now will be andmask, so again 1 wil be printed.
When j becomes 4 down to 1 a 0 will be printed, the last time, when j=0, a 1 will be printed, correctly printing the number 225 in binary.

Edited by ddanbe: typo


Thankyou ddanbe, I understand the loop part but still my question is not addressed, help..

1 & 1 = 1
0 & 1 = 0
1 & 0 = 0
0 & 0 = 0
|   |
|   \ this is a bit from the `andmask`
\ this is a bit from `n` 

Does this clears the mist?

Edited by ddanbe: correction


You have quite a few errors in your code.

Line 16 needs a "
Line 27 replace o with 0
Line 32 replace ! with 1

To see what's going on use a few temporary test prints.

Edited by vegaseat

Thankyou Vegaseat and ddanbe....

Dear Sir, I understand the rules of AND OR and XOR along with which variable contains what, the part thats slipping out of my grasp is the anding process....

Let me reiterate my problem using flow control

from main
line 9 Type declaration and assignment of 225 to num

line 11 function showbits is called with actual argument as 225
line 22 the formal arguement is stored in n
Line 29 Char variable j is assigned to int i value which is 7
Line 30 left shift 1 by 7 which is 10000000, in turn this is stored in                 andmask so now value in andmask is 10000000
Line 31 here comes my doubt....n contains the value of 225 which is                   11100001, this will be anded with andmask so,

        11100001 (n)
        10000000 (andmask)
        10000000 <<<< this value is stored in k

now with all these values 1s and 0s how its getting compared bit by bit in LINE 32

Is it the ternary operator you're having trouble with?

After the first and operation in the loop k=1 so
! gets printed
After the second and operation in the loop k=1 so
!! gets printed. Ther is no newline so ! gets on the sameline.
After the third and operation in the loop k=1 so
!!! gets printed.
After the fourth and operation in the loop k=0 so
!!!0 gets printed.
And so on until the whole number 225 is printed in binary as !!!0000!

Please take care of the excellent fault observations of vegaseat. Also line 10 could use an extra ".


Ook, so for each bit the loop thingy is happening like in "for" statement, excellent guys, thanks a lot, you all helping beginner people like me are worth more than diamonds, thanks all... am in gratitude.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.