# Thread: BitWise Newbie - Needs Urgent Help

1. ## 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. 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. Study the ^ operator.

4. 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.

5. Originally Posted by Salem
Study the ^ operator.

6. can someone post me on the code on the inversion part based on my program...???

7. > 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.

8. 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....

9. 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. 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.

11. 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

12. a = a ^ 0x0f;
Rather giving the game away I'm afraid.

13. 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.

14. 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;

15. 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.