Thread: 8 bit bit wise operation

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    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

  2. #2
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    I hope this example could help you:
    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[20] = {0};
     int decval = 0;
     
     printf("Please input an 8 Bit binary value: ");
     scanf("%8[01]", 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. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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 );
    Last edited by flp1969; 12-05-2019 at 04:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit-Wise expresions
    By ilans11il in forum C Programming
    Replies: 2
    Last Post: 07-07-2014, 04:08 AM
  2. What do i need network wise?
    By Logikal in forum Tech Board
    Replies: 1
    Last Post: 03-17-2010, 03:00 AM
  3. Bit wise operators
    By scrapper in forum C Programming
    Replies: 1
    Last Post: 02-14-2005, 09:01 AM
  4. What does this code do? (Bit wise operation)
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 10-07-2004, 01:15 PM

Tags for this Thread