Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,693 questions

57,001 answers

573 users

How to use the right shift >> in C

3 Answers

0 votes
#include <stdio.h>

char *toBinFormat(int n);

int main(int argc, char **argv) 
{ 
    int n = 128;
    
    for (int i = 0; i < 8; i++)
    {
        printf("%3d = %s\n", n, toBinFormat(n));
        n = n >> 1;
    }
    
    return(0);
}

char *toBinFormat(int n)
{
    static char binary_value[9]; // without static binary_value is a local 
                                 // array that disappear after return
    int i;
    
    for(i = 0; i < 8; i++)
    {
        binary_value[i] = n & 0x80 ? '1' : '0';
        n <<= 1;
    }
    binary_value[i] = '\0';

    return binary_value;
}

/*
run:

128 = 10000000
 64 = 01000000
 32 = 00100000
 16 = 00010000
  8 = 00001000
  4 = 00000100
  2 = 00000010
  1 = 00000001

*/

 



answered Jun 14, 2015 by avibootz
0 votes
#include <stdio.h>

char *toBinFormat(int n);

int main(int argc, char **argv) 
{ 
    int n = 32768;
    
    for (int i = 0; i < 16; i++)
    {
        printf("%5d = %s\n", n, toBinFormat(n));
        n = n >> 1;
    }
    
    return(0);
}

char *toBinFormat(int n)
{
    static char binary_value[17]; // without static binary_value is a local 
                                 // array that disappear after return
    int i;
    
    for(i = 0; i < 16; i++)
    {
        binary_value[i] = n & 0x8000 ? '1' : '0';
        n <<= 1;
    }
    binary_value[i] = '\0';

    return binary_value;
}

/*
run:

32768 = 1000000000000000
16384 = 0100000000000000
 8192 = 0010000000000000
 4096 = 0001000000000000
 2048 = 0000100000000000
 1024 = 0000010000000000
  512 = 0000001000000000
  256 = 0000000100000000
  128 = 0000000010000000
   64 = 0000000001000000
   32 = 0000000000100000
   16 = 0000000000010000
    8 = 0000000000001000
    4 = 0000000000000100
    2 = 0000000000000010
    1 = 0000000000000001

*/

 



answered Jun 14, 2015 by avibootz
0 votes
#include <stdio.h>

char *toBinFormat(int n);

int main(int argc, char **argv) 
{ 
    int n = 32768;
    
    printf("Dec   | Hex    | Bin\n");
    for (int i = 0; i < 16; i++)
    {
        printf("%5d | 0x%04X | %s\n", n, n, toBinFormat(n));
        n = n >> 1;
    }
    
    return(0);
}

char *toBinFormat(int n)
{
    static char binary_value[17]; // without static binary_value is a local 
                                 // array that disappear after return
    int i;
    
    for(i = 0; i < 16; i++)
    {
        binary_value[i] = n & 0x8000 ? '1' : '0';
        n <<= 1;
    }
    binary_value[i] = '\0';

    return binary_value;
}

/*
run:

Dec   | Hex    | Bin
32768 | 0x8000 | 1000000000000000
16384 | 0x4000 | 0100000000000000
 8192 | 0x2000 | 0010000000000000
 4096 | 0x1000 | 0001000000000000
 2048 | 0x0800 | 0000100000000000
 1024 | 0x0400 | 0000010000000000
  512 | 0x0200 | 0000001000000000
  256 | 0x0100 | 0000000100000000
  128 | 0x0080 | 0000000010000000
   64 | 0x0040 | 0000000001000000
   32 | 0x0020 | 0000000000100000
   16 | 0x0010 | 0000000000010000
    8 | 0x0008 | 0000000000001000
    4 | 0x0004 | 0000000000000100
    2 | 0x0002 | 0000000000000010
    1 | 0x0001 | 0000000000000001

*/

 



answered Jun 15, 2015 by avibootz
...