# Thread: 8 bit bit wise operation

1. ## 8 bit bit wise operation

I understand bitwise operator in c language we can get decimal, hex input from user

Any idea how to get only 8 bit data from user to perform bitwise operations

for examples

0000 1111 << 1 Code:
```/* Example for scanf to limit the number of characters where read into a string
In this example scanf reads only characters are stored between the square brackets
If the first character who is not in between the square brackets, input ends.
Example input is: 0101a
The result is 0101 (a not in list). decimal value is 5.
The numer 8 after the percent sign limits the number of characters to 8. */
#include <stdio.h>
#include <string.h> // strlen

// bintoint converts a binary(stored in a c-string) into a int-value,
// return value is the int-value
int bintoint(char binval[])
{
int i, lg, dumza = 0, testvalue = 1;

lg = strlen(binval);

testvalue <<= (lg - 1);

for (i = 0; i < lg; i++)
if ((binval[i] == 'I') || (binval[i] == '1')) dumza += testvalue >> i;

return dumza;
}

int main(int argc, char **argv)
{
char binval = {0};
int decval = 0;

printf("Please input an 8 Bit binary value: ");
scanf("%8", binval);  // limit of characters where read is 0. Ends by the first character where is not 0 or 1
printf("input was: %s\n", binval);

decval = bintoint(binval);

printf("decimal value: %d\n", decval);

return 0;
}
``` 3. Code:
```// Assuming string is less then 33 chars long (excluding the '\0').
// Assuming, also, each char is either '0' or '1'.
unsigned int bin2int(char *str)
{
unsigned int n = 0;

while ( *str )
{
n <<= 1;
n |= (*str - '0');
str++;
}

return n;
}```
Or you can use strtoul() function:
Code:
```unsigned int n;

n = strtoul( str, NULL, 2 );``` Popular pages Recent additions bit, bitwise, idea, perform, user 