Thread: Give me a reason!

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    Question Give me a reason!

    can anybody explain this simple code for me.
    I don't understand how the ">>" and "&" work. Thanks a lot!!

    #include<stdio.h>

    #define A_BYTES_SIZE 8
    #define A_WORDS_SIZE 16

    void main (void)
    {
    int ValueToConvert=256,
    CurrentBitPosition=1;
    unsigned int BitMask=1;

    printf("The following value %d,\n",ValueToConvert);
    printf("in binary form looks like:");

    while (CurrentBitPosition<=A_WORDS_SIZE){
    if ( (ValueToConvert>>(A_WORDS_SIZE-CurrentBitPosition) )& BitMask)
    printf("1");
    else
    printf("0");
    if( CurrentBitPosition ==A_BYTES_SIZE)
    printf(" ");
    CurrentBitPosition;
    }
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A = A >> B

    Shift the bits in A to right over B positions.

    1101 >> 2 = 0011

    A = A & B

    Bitwise AND-operation.

    0 AND 0 = 0
    0 AND 1 = 0
    1 AND 1 = 1

    1101 & 1011 = 1001

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if( CurrentBitPosition ==A_BYTES_SIZE)
    >printf(" ");
    >CurrentBitPosition;

    And I think this should be:
    CurrentBitPosition++;

    as you are checking each bit for a 1 or 0, starting with the leftmost (most significant) bit.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    well if it is copied directly
    #include (need a space) <stdio.h>
    Code:
    #include <stdio.h> 
    
    #define A_BYTES_SIZE 8 
    #define A_WORDS_SIZE 16 
    
    int main() 
    { 
    	int ValueToConvert=256, 
    	CurrentBitPosition=1; 
    	unsigned int BitMask=1; 
    
    	printf("The following value %d,\n",ValueToConvert); 
    	printf("in binary form looks like:"); 
    
    	while (CurrentBitPosition<=A_WORDS_SIZE){ 
    		if ( (ValueToConvert>>(A_WORDS_SIZE-CurrentBitPosition) )& BitMask) 
    			printf("1"); 
    		else 
    		printf("0"); 
    		if( CurrentBitPosition ==A_BYTES_SIZE) 
    			printf(" "); 
    	CurrentBitPosition; 
    	} 
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    Smile Thanks a lot

    Thanks a lot for your explainantion, and it's of great help to me.
    Swoopy , maybe you're right. I think it's a typo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please give ur point of view
    By clover in forum C Programming
    Replies: 2
    Last Post: 05-04-2004, 03:56 PM
  2. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  3. My thread stops for no reason
    By lectrolux in forum C Programming
    Replies: 7
    Last Post: 05-21-2003, 07:56 AM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM