473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cannot understand function logic

542 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include <stdio.h>
  3.  
  4. void print_binary(short input_field);       //declare function print_binary
  5.  
  6. int  main (){
  7.     printf("This program prints the binary equivalent of a short integer\n");
  8.     printf("by AND_ing it with Ox8000 and assigning true (1) if the result\n");
  9.     printf("is greater than 0 otherwise false (0)\n");
  10.  
  11.     short n ;                               // declare n as a short integer type
  12.  
  13.     do{                                     
  14.         printf("\nEnter a short integer "); 
  15.         printf("(0 to quit): ");            
  16.         scanf("%hd",&n);                    
  17.                 print_binary(n);            // call function
  18.   }  while (n);                             // continue to loop while n > 0
  19. }    
  20. void print_binary(short input_field) {      // function print_biary
  21.     int i = 1,bit_set;                      //declare i (initialised to 1)
  22.                                             // and bit_set
  23.  
  24.        while (i <=16) {                            //loop while i <= 16. get bit AND result
  25.            bit_set=((0x8000 & input_field) > 0);   //this evaluates to 0 or 1.
  26.                                                    // equiv of Ox8000 = 1000,0000,0000,0000
  27.            printf("%hd",bit_set);                   //if 0 it prints bool 1 else 0
  28.  
  29.            input_field <<=  1;// move 1 bit to the left
  30.            i++;
  31.  
  32.         }
  33. }   
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.
Nov 17 '11 #1

✓ answered by Rabbit

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

4 1881
Rabbit
12,516 Expert Mod 8TB
I'll simplify the example.
Expand|Select|Wrap|Line Numbers
  1. n = 3;
  2. while (i <= 4) {
  3.    bit_set=((0x8 & input_field) > 0);
  4.    input_field <<=  1;
  5.    i++;
  6. }
3 in binary is 0011.
0x8 in binary is 1000.

Expand|Select|Wrap|Line Numbers
  1. i = 1
  2. input_field = 0011
  3. bit_set = 1000 & 0011 = 0000 > 0 = 0
  4. input_field <<= 1 = 0110
Expand|Select|Wrap|Line Numbers
  1. i = 2
  2. input_field = 0110
  3. bit_set = 1000 & 0110 = 0000 > 0 = 0
  4. input_field <<= 1 = 1100
Expand|Select|Wrap|Line Numbers
  1. i = 3
  2. input_field = 1100
  3. bit_set = 1000 & 1100 = 1000 > 0 = 1
  4. input_field <<= 1 = 1000
Expand|Select|Wrap|Line Numbers
  1. i = 4
  2. input_field = 1000
  3. bit_set = 1000 & 1000 = 1000 > 0 = 1
Nov 17 '11 #2
whodgson
542 512MB
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?
Nov 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
Oops, my fault, you're right, it is 0000. I fixed my example above.
Nov 18 '11 #4
whodgson
542 512MB
Yes...thanks Rabbit...like taking candy from kids
Nov 18 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars...
2
by: None | last post by:
Hello, 1. The prototype of function signal in signal.h: void (*signal(int sig, void (*func)(int)))(int); is some complex to me. Would you please explain it to me in detail with the C...
5
by: rashmi | last post by:
Hello All, tp.c:107: error: syntax error before '*' token tp.c:108: warning: function declaration isn't a prototype tp.c:121: error: syntax error before '*' token tp.c:122: warning: function...
0
by: antonyliu2002 | last post by:
The full code is pasted below. It looks scarily long, but it's pretty simple. It is adapted from a sample code at MSDN. If you run it, and check it out from your browser, you'll see: ...
17
by: Gladiator | last post by:
When I am trying to execute a program from "The C Programming Language" by Dennis Ritchie, I tried to run the following program.I am using Dev++ as a compiler software. The Program is presented...
2
by: Ming | last post by:
I do not understand the sample code on php.net for curl_multi_exec. Any help? Many thanks! <?php // create both cURL resources $ch1 = curl_init(); $ch2 = curl_init(); // set URL and other...
11
by: paradox6996 | last post by:
I'm trying to convert someones cereal weight from ounces to metric tons, and how many boxes it would take to make a metric ton. I suck at converting weights and I wanted to know if the logic in my...
3
by: Hazza | last post by:
Hi, I am using PHP and mysql to create a website. I am fairly new to PHP, and thus am grateful to anyone who helps! Firstly I am running a homepage, that displays additional content if a user...
2
by: sam.barker0 | last post by:
Hi , I am having 3 functions.When I step through when func b returns to funca.it throws an error "cannot find function bounds" funca() { .... ... funcb(); }
9
by: reachmsn | last post by:
Hi, At the url http://www.python.org/doc/essays/graphs.html there is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.