# BitWise Newbie - Needs Urgent Help

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-25-2007
frodonet
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(); }```
• 09-25-2007
robatino
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.
• 09-25-2007
Salem
Study the ^ operator.
• 09-25-2007
frodonet
Quote:

Originally Posted by robatino
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.
• 09-25-2007
frodonet
Quote:

Originally Posted by Salem
Study the ^ operator.

i read 2 books on it already and i'm going crazy :devil:
• 09-25-2007
frodonet
can someone post me on the code on the inversion part based on my program...???
• 09-25-2007
robatino
> 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.
• 09-26-2007
frodonet
Quote:

Originally Posted by robatino
> 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....
• 09-26-2007
robatino
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.
• 09-26-2007
frodonet
Quote:

Originally Posted by robatino
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.
• 09-26-2007
matsp
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
• 09-26-2007
Salem
a = a ^ 0x0f;
Rather giving the game away I'm afraid.
• 09-26-2007
robatino
Quote:

Originally Posted by Salem
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.
• 09-26-2007
PrashantVaidwan
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;```
• 09-26-2007
robatino
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last