Cannot understand function logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    Cannot understand function logic

    the following is a small program from Brian Overland`s book "C++ In Plain English" It converts a number from decimal format to binary format as an example of bit shifting.
    My problem is that i cannot follow the logic in the function.
    here is the program:
    Code:
    #include<iostream>
    #include <stdio.h>
    
    void print_binary(short input_field);       //declare function print_binary
    
    int  main (){
        printf("This program prints the binary equivalent of a short integer\n");
        printf("by AND_ing it with Ox8000 and assigning true (1) if the result\n");
        printf("is greater than 0 otherwise false (0)\n");
        
        short n ;                               // declare n as a short integer type
        
        do{                                     
            printf("\nEnter a short integer "); 
            printf("(0 to quit): ");            
            scanf("%hd",&n);                    
                    print_binary(n);            // call function
      }  while (n);                             // continue to loop while n > 0
    }    
    void print_binary(short input_field) {      // function print_biary
        int i = 1,bit_set;                      //declare i (initialised to 1)
                                                // and bit_set
        
           while (i <=16) {                            //loop while i <= 16. get bit AND result
               [B]bit_set=((0x8000 & input_field) > 0);[/B]   //this evaluates to 0 or 1.
                                                       // equiv of Ox8000 = 1000,0000,0000,0000
               printf("%hd",bit_set);                   //if 0 it prints bool 1 else 0
               
               input_field <<=  1;// move 1 bit to the left
               i++;
                  
            }
    }
    specifically AND (ing) OX8000 with an integer (<OX8000)
    produces 0000 0000 0000 0000 so how can bit_set >0 ever be true (but even so how does it produce the binary equivalent of n)
    I would appreciate however much excruciating detail is forthcoming.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I'll simplify the example.
    Code:
    n = 3;
    while (i <= 4) {
       bit_set=((0x8 & input_field) > 0);
       input_field <<=  1;
       i++;
    }
    3 in binary is 0011.
    0x8 in binary is 1000.

    Code:
    i = 1
    input_field = 0011
    bit_set = 1000 & 0011 = 0000 > 0 = 0
    input_field <<= 1 = 0110
    Code:
    i = 2
    input_field = 0110
    bit_set = 1000 & 0110 = 0000 > 0 = 0
    input_field <<= 1 = 1100
    Code:
    i = 3
    input_field = 1100
    bit_set = 1000 & 1100 = 1000 > 0 = 1
    input_field <<= 1 = 1000
    Code:
    i = 4
    input_field = 1000
    bit_set = 1000 & 1000 = 1000 > 0 = 1

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      thanks Rabbit
      hmmm you say that (binary) 1000 & 0011 = 0011?
      my book says 1000 & 0011 = 0000 or 0!
      so does my calculator.
      can we discuss further?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Oops, my fault, you're right, it is 0000. I fixed my example above.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Yes...thanks Rabbit...like taking candy from kids

          Comment

          Working...