Thread: printing most sig byte...??

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    13

    printing most sig byte...??

    I have a pointer p that contains a valid address and am trying to print the MS 8 bits... since there isn't a way to do that in printf that i know of, i'm using a loop and it looks like...

    Code:
     for(i=0; i<8; i++)
        {
          if(*p & 0x80)
            printf("1");
          else
            printf("0");
    
          *p<<1;
    
        }
    however this either prints either 8 1's or 8 0's... I know that it's just not the way the numbers are b/c i have functions that print the numbers in decimal and hex and those work fine... any ideas?

    John

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: printing most sig byte...??

    *p<<1;
    Instead, try
    *p <<= 1;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    hmm, it says Bus error (core dumped) that's weird. considering it works fine if you print in hex or decimal

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    can you only perform shift operations on ints, can youdo them on chars?

    John

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about something like this?
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void foo(void *a)
    {
       union uType
       {
          void *p;
          unsigned char b [ sizeof(void*) ];
       } var;
       size_t i = sizeof(var.b)/sizeof(*var.b);
       var.p = a;
       printf("var.p  =  %p\n", var.p);
       do
       {
          size_t j = CHAR_BIT;
          printf("var.b[%lu]: ", (long unsigned)--i);
          do
          {
             putchar(var.b[i] & (1 << --j) ? '1' : '0');
          } while ( j );
          putchar('\n');
       } while ( i );
    }
    
    int main(void)
    {
       int x = 42;
       foo(&x);
       return 0;
    }
    
    /* my output
    var.p  =  0012FF88
    var.b[3]: 00000000
    var.b[2]: 00010010
    var.b[1]: 11111111
    var.b[0]: 10001000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by myjay
    hmm, it says Bus error (core dumped) that's weird. considering it works fine if you print in hex or decimal
    Are you trying to run that code against an array or a string literal? If it's the latter, it won't work, as it modifies the data.

    So, if you have this:
    char *p = "blah";
    change it to
    char p[] = "blah";
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:45 AM