Thread: BitWise Newbie - Needs Urgent Help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    45

    BitWise Newbie - Needs Urgent Help

    So how do you exactly do this ????

    A function that zeroes the upper 4 bits of an unsigned char x and inverts its lower 4
    bits.

    EG : 170 binary is 10101010

    Then result : 00000101

    I managed until 00001010, but i unable to do the inversion to 00000101. Need urgent help, i'm going crazy with this lab tutorial ....

    Code:
    #include <stdio.h>
    
    main()
    {
          int i,c;
          unsigned int a, b[8];
          
          printf("\nEnter a decimal value: ");
          scanf("%d",&a);
          c=a;
          
          for(i=0;i<=7;i++)
          {
                b[i]=c%2;
                c=c/2;
          }
    
          printf("\nInput %d is ",a);
          for(i=7;i>=0;i--)
          {
                printf("%d",b[i]);
          }
          printf(" in binary.\n\n");
          for(i=7;i>=4;i--)
          {
              b[i] = b[i] >> 1;
          }      
          printf("Result :");
          for(i=7;i>=0;i--)
          {
                
                printf("%d", b[i]);
          }
          getchar();
          getchar();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It's easy - first do the invert, then the zeroing. Instead of inverting just the lower 4 bits, you can invert all of them, since the result is the same. BTW, an unsigned char is guaranteed only to have at least 8 bits, not exactly 8.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Study the ^ operator.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by robatino View Post
    It's easy - first do the invert, then the zeroing. Instead of inverting just the lower 4 bits, you can invert all of them, since the result is the same. BTW, an unsigned char is guaranteed only to have at least 8 bits, not exactly 8.
    i have did the invert function but my output is mumbo jumbo...

    i've tried this with b[i] = ~b[i]

    it just comes out -1. i don know why....

    is there any part of my program where i declared it wrong.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Salem View Post
    Study the ^ operator.
    i read 2 books on it already and i'm going crazy

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    can someone post me on the code on the inversion part based on my program...???

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > it just comes out -1. i don know why....

    You need to use &#37;u to correctly print the value of an unsigned int, not %d which is for (signed) int. Try that.

    Edit: The same goes for scanf(). Study carefully the conversion characters for printf() and scanf() for unsigned vs. signed types.
    Last edited by robatino; 09-26-2007 at 12:03 AM.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by robatino View Post
    > it just comes out -1. i don know why....

    You need to use %u to correctly print the value of an unsigned int, not %d which is for (signed) int. Try that.

    Edit: The same goes for scanf(). Study carefully the conversion characters for printf() and scanf() for unsigned vs. signed types.
    i have used %u for print f and scanf still does not work.

    when i tried the b[i] = ~b[i]

    my output is 42531145614879798456 bla bla....

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The maximum value of a 32-bit unsigned int (with no padding bits, don't worry about what those are right now) is 2^32 - 1 == 4294967295, which is what ~0U should be equal to in that case.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by robatino View Post
    The maximum value of a 32-bit unsigned int (with no padding bits, don't worry about what those are right now) is 2^32 - 1 == 4294967295, which is what ~0U should be equal to in that case.
    i see your point on why i getting that very big number, but i don see where i did wrong.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    printf will treat an unsigned char as a unsigned int, and if you do something like "printf("%d", ~b);", then you may get the unsigned int version of ~b, which would be a rather large number.

    You can do the required stuff with one invert and one and operation, by the way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    a = a ^ 0x0f;
    Rather giving the game away I'm afraid.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Salem View Post
    a = a ^ 0x0f;
    Rather giving the game away I'm afraid.
    That inverts the lower 4 bits, but leaves the higher bits unchanged, instead of zeroing them.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    have a try with the following sequence
    Code:
    unsigned char p=170;
    p=~p;
    p=p<<4;
    p=p>>4;
    printf("&#37;d \n",p);
    or
    Code:
    p=~p;
    p=p & 0x0f;
    Last edited by PrashantVaidwan; 09-26-2007 at 03:10 AM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    There's a simpler one-line solution involving ~, &, and 0x0f.

    Edit: Actually, your original solution can be written on one line too. The OP was assuming that an unsigned char has exactly 8 bits, when it only has to be >= 8. Your solution inverts the lowest 4 bits and zeros the highest 4, while mine inverts the lowest 4 bits and zeros all the higher bits. If the number of bits is 8 they are equivalent.
    Last edited by robatino; 09-26-2007 at 03:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 06:45 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. Urgent homework help for NEWBIE
    By Kazasan in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 04:23 PM
  4. Characters into bitwise ints
    By Code Zer0 in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2003, 08:34 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM