Thread: Bit Display.

  1. #1
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19

    Bit Display.

    Code:
    mask =  0x80;
    for(i = 0; i < 8; i++)
    {
    	if((testvalue & mask)!= 0)
          {
    			cout<<"1";
          }
    		else
          {
    			cout<<"0";
          }
    		mask = mask >> 1;
    
    }
    the code displayed was for displaying the bits in a char,however it doesn't work. Please help me with the code.
    Thanx.

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    // Assuming you want to look at bit 7
    int bit_num = 7;
    
    // Shifts the number so the bit you want is on the left
    // so we can use bitwise and on it and it will either be 1 or 0
    int the_bit = ( ( testvalue >> bit_num ) & 1 );
    
    cout << the_bit;
    So to do a full byte

    Code:
    for ( int loop = 7; loop >= 0; loop-- ) {
    
        int the_bit = ( testvalue >> loop ) & 1;
        cout << the_bit;
    
    }
    [edit]
    Also this really should have been posted in the c++ board.
    Last edited by Vicious; 09-15-2004 at 02:00 AM.
    What is C++?

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    >Also this really should have been posted in the c++ board.
    why?

  4. #4
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19
    Thanx for the program, i hav another one working too. Its just that I wanted to know what was wrong with the code I posted above.
    Also by doing "testvalue >> loop " I'm changing the testvalue which may affect other values that operate on test value.
    Hence i wanted to shift the bits in the mask = 0x80.

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    @sand_man: Because his code is C++ code...

    @Hypercase:
    Also by doing "testvalue >> loop " I'm changing the testvalue which may affect other values that operate on test value.
    Nope test value will still have its original value because you havent re-assigned it. For it to change you would have to do something like testvalue >>=loop

    And Ive been looking at your code and I bet it always outputs 0's doesnt it? I think it has something to do with how you assign mask.. Im not sure yet... I am still trying to figure it out

    [edit]

    I tried this

    Code:
    #include <iostream>
    
    int main ( void )
    {
    	
    	int mask = 0x80;
    	char testvalue = 0xFA;
    	
    	for ( int loop = 0; loop < 8; loop++ ) {
    		
    		if ( ( testvalue & mask ) != 0 ) {
    			
    			std::cout << "1";
    			
    		}
    		
    		else std::cout << "0";
    		
                    // Same thing as mask = mask >> 1
    		mask >>= 1;
    		
    	}
    	
    	return 0;
    	
    }
    And it worked... odd. Maybe if you show some more code I can figure it out.
    Last edited by Vicious; 09-15-2004 at 02:40 AM.
    What is C++?

  6. #6
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19
    Thanx Vicious, that was very helpful.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Vicious
    And Ive been looking at your code and I bet it always outputs 0's doesnt it? I think it has something to do with how you assign mask.. Im not sure yet... I am still trying to figure it out
    ...snip...
    And it worked... odd. Maybe if you show some more code I can figure it out.
    You can see what's going on if you display the bit pattern:
    Code:
    #include <stdio.h>
    void draw ( int v, int n )
    {
            int x;
            for( x = 0; x < n; x++ )
            {
                    printf("%d", (v & (1 << ((n-1)-x))) != 0 );
            }
    }
    int main ( void )
    {
            int mask = 0x80;
            int loop;
            char testvalue = 0xFA;
    
            for( loop = 0; loop < 8; loop++ )
            {
                    printf("%3d ", mask );
                    draw( mask, 8 );
                    printf(": %d\n", (testvalue &mask) != 0 );
                    mask >>= 1;
            }
            return 0;
    }
    
    
    /*
    128 10000000: 1
     64 01000000: 1
     32 00100000: 1
     16 00010000: 1
      8 00001000: 1
      4 00000100: 0
      2 00000010: 1
      1 00000001: 0
    */
    On the left is the value of the mask. The bit pattern of the mask is in the middle. On the right is the result of it.

    [edit]
    I changed the order the bits display (left to right, instead of right to left), so perhaps it'll be a bit clearer.
    [/edit]


    Quzah.
    Last edited by quzah; 09-15-2004 at 11:20 AM.
    Hope is the first step on the road to disappointment.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    just for fun - here's an easy way to display an integer in any base (even user defined):

    Code:
    char * tobase(int number, char * buffer, const char * digits) {
     size_t index, base = strlen(digits);
     char * b = buffer;
     if(base) {
      while(number) {
       index = number % base;
       *b++ = digits[index];
       number /= base;
       }
      }
     *b = 0;
     return strrev(buffer);
     }
    
    
    
    int main(int, char ** argv) {
     int input, size = 256;
     char * dec = "0123456789",
          * hex = "0123456789ABCDEF",
          * oct = "01234567",
          * bin = "01",
          * base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
          buffer[size+1];
     while(printf(" Enter an integer to convert >") && *fgets(buffer, size, stdin) != '\n') {
      input = atoi(buffer);
      printf(" Control: %d\n", input);
      printf(" Decimal: %s\n", tobase(input, buffer, dec));
      printf(" Hexidecimal: %s\n", tobase(input, buffer, hex));
      printf(" Octal: %s\n", tobase(input, buffer, oct));
      printf(" Binary: %s\n", tobase(input, buffer, bin));
      printf(" Base 36: %s\n", tobase(input, buffer, base36));
      }
     return 0;
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    I wouldn't exactly say any base. Since your method is dependant on a digit being only one byte, you will be limited to a maximum of 256-base (or is it 255-base?).

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Frobozz
    I wouldn't exactly say any base. Since your method is dependant on a digit being only one byte, you will be limited to a maximum of 256-base (or is it 255-base?).
    hmm, interesting point.

    actually, I overlooked a couple of things, too: the loop is set up wrong and the function doesn't account for negative numbers!

    lemme try again:

    Code:
    char * tobase(int number, char * buffer, const char * digits) {
     size_t index, base = strlen(digits);
     char * b = buffer;
     int isNeg = 0;
     if(number < 0) {
      isNeg = 1;
      number = -number;
      }
     if(base) {
      do{
       index = number % base;
       *b++ = digits[index];
       number /= base;
       } while(number);
      }
     if(isNeg) {
      *b++ = '-';
      }
     *b = 0;
     return strrev(buffer);
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Hypercase
    the code displayed was for displaying the bits in a char,however it doesn't work. Please help me with the code.
    Thanx.

    What do you mean "doesn't work"? Are you sure that testvalue has the value that you think it has? Your code works perfectly for me.

    Run the following and see what you get.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
      char testvalue;
      int mask;
    
      for (int j = -128; j <= 127; j++) {
        testvalue = j;
        cout << (int)testvalue << " decimal = ";
        mask = 0x80;
        for(int i = 0; i < 8; i++) {
          if((testvalue & mask)!= 0) {
            cout << "1";
          }
          else {
            cout << "0";
          }
          mask = mask >> 1;
        }
        cout << " binary" << endl;
      }
      return 0;
    }
    (Redirect the output to a file so that you can inspect the results, or change the outer loop to see whatever values you are interested in.)
    Regards,

    Dave
    Last edited by Dave Evans; 09-16-2004 at 08:45 AM.

  12. #12
    Registered User msummers's Avatar
    Join Date
    Apr 2008
    Location
    Southern California
    Posts
    11
    Quote Originally Posted by Sebastiani View Post
    hmm, interesting point.

    actually, I overlooked a couple of things, too: the loop is set up wrong and the function doesn't account for negative numbers!

    lemme try again:

    Code:
    char * tobase(int number, char * buffer, const char * digits) {
     size_t index, base = strlen(digits);
     char * b = buffer;
     int isNeg = 0;
     if(number < 0) {
      isNeg = 1;
      number = -number;
      }
     if(base) {
      do{
       index = number % base;
       *b++ = digits[index];
       number /= base;
       } while(number);
      }
     if(isNeg) {
      *b++ = '-';
      }
     *b = 0;
     return strrev(buffer);
     }
    The code now works after this:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <limits.h>
    #include <float.h>
    
    char * tobase(int number, char * buffer, const char * digits){
    	size_t index, base = strlen(digits);
    	char * b = buffer;
    	int isNeg = 0;
    	if(number < 0){
    		isNeg = 1;
    		number = -number;
    	}
    	if(base){
    		do{
    			index = number % base;
    			*b++ = digits[index];
    			number /= base;
    		} while(number);
    	}
    	if(isNeg){
    		*b++ = '-';
    	}
    	*b = 0;
    	return _strrev(buffer);
    }
    int main(int, char ** argv){
    	int input;
    	int size = 256;
    	char buffer[256];
    	char * dec = "0123456789",
    		 * hex = "0123456789ABCDEF",
    		 * oct = "01234567",
    		 * bin = "01",
    		 * ter = "012",
    		 * mayan = "0123456789ABCDEFGHIJKLMNOPQRST",
    		 * base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	while(printf("Enter an integer to convert >") && *fgets(buffer, size, stdin) != '\n'){
    		input = atoi(buffer);
    		printf("Control: %d\n", input);
    		printf("Decimal: %s\n", tobase(input, buffer, dec));
    		printf("Hexidecimal: %s\n", tobase(input, buffer, hex));
    		printf("Octal: %s\n", tobase(input, buffer, oct));
    		printf("Binary: %s\n", tobase(input, buffer, bin));
    		printf("Tertiary: %s\n", tobase(input, buffer, ter));
    		printf("Mayan: %s\n", tobase(input, buffer, mayan));
    		printf("Base 36: %s\n", tobase(input, buffer, base36));
    		}
    	return 0;
    }
    Last edited by msummers; 04-14-2008 at 04:31 PM. Reason: Have fix the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  2. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 09:58 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM