Thread: c bit shift left and right

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    c bit shift left and right

    i'm reading a book on c bit shift i trying to understand what's going on in this c program bit shift i uploaded picture of the result
    someone can explain me thanks very much
    i entered bit1 00000001 and bit2 10000000
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        printf("c bit shift\n");
        unsigned int bit1;
        unsigned int bit2;
        unsigned int result;
        
        printf("Enter first number : ");
        scanf("%d",&bit1);
        printf("Enter second number : ");
        scanf("%d",&bit2);
        
        result = bit1 << bit2;
        printf("left ........ bit result is bit1 %d bit2 %d result %d\n",bit1,result);
        
        result = bit1 >> bit2;
        printf("right ........ bit result is bit1 %d bit2 %d result %d\n",bit1,bit2,result);
        
        return 0;
    }
    Attached Images Attached Images c bit shift left and right-schermata-del-2016-02-27-19-38-32-jpeg 

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Shifting more bits than what can fit inside a variable is undefined behavior. In your case, you're shifting ten million bits to the left and right...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Bangalore, Karnataka, India
    Posts
    34
    Quote Originally Posted by ssm View Post
    someone can explain me thanks very much
    i entered bit1 00000001 and bit2 10000000
    Lets first concentrate on line# 17:

    Code:
    result = bit1 << bit2;
    The actual values will be:
    result =00000001 << 1000000 (All values are in decimal and not in binary)

    What exactly happens when you do a shift operation?
    The left value (bit1) will be shifted to left (<< because of the left shift operator) by number times the right value (bit2).

    Lets take the bit2 value as 2 (and not 10000000, just to understand the shifting operation better). Below will be the expanded operation:

    result = 00000001 << 2 (values are in decimal)

    So, the final result will be :
    result = 4 (0100)

    Explanation:
    Expand the bit1 into binary values.
    1 will be 0001 (4-bits, all other bits will be zero so I have not shown at all). If you will left shift this value by a count of 2 (value of bit2) then what will happen:
    0001 will become 0100 (The whole 32-bit binary value will be shifted towards left 2 times and 2 zeros have been added to the right by the shifting hardware automatically)


    Now, Considering the unsigned int is of 32-bit value, the bit1 value can be shifted a max of 32-bits only. Beyond the number of shifting the operation will be undefined.

    Similar shifting will be carried out on the right shift. The only difference will be: The entire 32-bit value will be shifted towards right and either 1 or 0 will be borrowed on the left side depending on signed or unsigned shifting.

    I hope this will help you understand the shifting operation better.

    If you still have some doubt then do convey them on a reply.

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    your explanation is so good i understood very well explain me something about this function to check if bit is set or not
    Code:
    typedef unsigned char byte;
    
    
    int isbitSet(byte ch,unsigned int i){
    	unsigned char mask = 1 << i;
    	return mask & ch;
    }

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    instead if typing in binarary numbers that scanf don't understand,Have it ask,
    what bit to set (0-31)? // BIT0 = %...0001
    How many bit to left shift this value (0-31)?

    If (input2>input1) print warning result will overflow/carry;
    else print (1<<input1<<input2);
    Last edited by tonyp12; 02-29-2016 at 01:21 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Code:
        unsigned char mask = 1 << i;
        return mask & ch;
    What mask is: left shift is like multiplying by two, so if you take 1 and shift it left, you end up with a power of 2 (2i).

    In binary, this means that one of the place values will be set on. Bitwise-and will check every bit of its two operands. If both bits are on, then the bit is on in the result. Since mask only has 1 bit on, you may get answers that are powers of 2, like , 4, 16, 32 or slightly larger if the bit is on, or zero if the bit is off.

    What truth is in C: A nonzero answer is considered true, while 0 is false. So, you could use isbitSet() in an if statement:
    Code:
     if (isbitSet('l', 3) != 0)
    Which, means that 'l' has the 8th place value set, so part of 'l's value is 23

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    sorry for if i am insisting with these questions
    if i want 3 <<= 2 so 00000011 <<= 00000010 so the result is 00001110 = 14 is it right ? but when i run on codelite the result is 12 why ??

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by ssm View Post
    sorry for if i am insisting with these questions
    if i want 3 <<= 2 so 00000011 <<= 00000010 so the result is 00001110 = 14 is it right ? but when i run on codelite the result is 12 why ??
    3 <<= 2

    (0 left-shifts) = 00000011 = 3
    (1 left-shift ) = 00000110 = 6
    (2 left-shifts) = 00001100 = 12

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    thanks very much now left shift and right shift is clear for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shift Left, Shift Right Question
    By congi in forum C Programming
    Replies: 1
    Last Post: 01-28-2015, 10:45 AM
  2. Left shift question
    By patishi in forum C Programming
    Replies: 8
    Last Post: 09-17-2013, 06:29 PM
  3. bit (or) operation,left shift
    By Hassan Ahmed in forum C Programming
    Replies: 5
    Last Post: 07-19-2013, 02:15 PM
  4. Left shift a string
    By Tigers! in forum C Programming
    Replies: 10
    Last Post: 08-16-2009, 11:58 PM
  5. Left Shift
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 11:15 AM