Thread: bit (or) operation,left shift

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    bit (or) operation,left shift

    output is 12480
    (0) is the problem i expect 16
    i trace this code but i have problem at i=5
    Code:
    #include<stdio.h>
    int main()
    {
     
    char c=48;// char c='0';
     
        int i, mask=1;
        for(i=1; i<=5; i++)
        {
            printf("%c",c|mask);
            
            mask = mask<<1;
        }
       
       
       
       
            system("pause");
        return 0;
    }
    c=0,mask=1
    i=1
    print 1
    c=0,mask=2

    i=2
    print 2
    c=0,mask=4

    i=3
    print 4
    c=0,mask=8

    i=4
    print 8
    c=0,mask=16

    i=5
    print (0 bit or 16)
    2pown 128 64 32 16 8 4 2 1
    bit id 7 6 5 4 3 2 1 0
    c 0 0 0 0 0 0 0 0
    mask 0 0 0 1 0 0 0 0
    0 bitor 16 0 0 0 1 0 0 0 0


    print 16 not 0 ???

    c=0,mask=32

    return 0 // end of main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > (0) is the problem i expect 16
    If you want 16, then you need to

    - make c an int.
    - initialise it to 0 and not '0'
    - use %d to print it.

    You're just not going to get 16 printed with a single letter format like %c.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    replace the %c with a %d\n to see what the integer value is of the expression you are printing. Then you'll understand.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    13
    i need to print char has value of 16(ASCII)
    Code:
    char c=16;
    printf("%c",c);
    out of this code is
    ASCII code of 16 (data link escape)
    bit (or) operation,left shift-ascii-16-png

    all i expect is to get the ASCII of (char) but (not zero)

    how the prog print "zero" instead ASCII code
    thanks for your time

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Hassan Ahmed View Post
    how the prog print "zero" instead ASCII code
    You start with c = 48, which is hex 30 or binary 00110000. Showing the numbers in binary, after i == 4, then mask = 00010000 and c | mask = 00110000 | 00010000 = 00110000 (no change since the bit is already set). The same thing happens for the next step where mask = 00100000.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    thanks for your help

    i get it the output
    ASCII Table and ASCII Code - Programming - Fclose Tutorials
    ascii table
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      char c=48;// char c='0';
      
        int i, mask=1;
        for(i=0; i<=7; i++)
        {
            char result=c|mask;
            printf("%c\n",result);
            printf("ascii code of c=%d\n",result);
            mask = mask<<1;
        }
      
      system("PAUSE");    
      return 0;
    }
    output
    1
    ascii code of c=49
    2
    ascii code of c=50
    4
    ascii code of c=52
    8
    ascii code of c=56
    0
    ascii code of c=48
    0
    ascii code of c=48
    p
    ascii code of c=112

    ascii code of c=-80
    Press any key to continue . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shift operation on a negative number.
    By avee137 in forum C Programming
    Replies: 4
    Last Post: 09-02-2010, 02:00 PM
  2. Left shift a string
    By Tigers! in forum C Programming
    Replies: 10
    Last Post: 08-16-2009, 11:58 PM
  3. Left Shift
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 06-17-2007, 11:15 AM
  4. left shift operator!!!!
    By theblackhat in forum C Programming
    Replies: 2
    Last Post: 10-02-2004, 02:07 AM
  5. Left Shift problem
    By Mox in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2001, 03:58 AM