简体   繁体   English

接受用户输入的位运算符

[英]Bitwise operator with taking input from user

#include<stdio.h>

int main()
{
    unsigned char a,b;
   
    printf("Enter first number : ");
    scanf("%d",&a);
    printf("Enter 2nd number : ");

    scanf("%d",&b);
    printf("a&b = %d\n", a&b);

    printf("a|b = %d\n", a|b); 

    printf("a^b = %d\n", a^b);
    
    printf("~a = %d\n",a = ~a);
    printf("b<<1 = %d\n", b<<1); 

    printf("b>>1 = %d\n", b>>1); 

    return 0;
}

i am taking input from user but i am getting wrong output how i modify i***我正在接受用户的输入,但我弄错了 output 我如何修改我 ***

error


These calls of scanf scanf的这些调用

scanf("%d",&a);

and

scanf("%d",&b);

use an incorrect format specification.使用不正确的格式规范。

The variables a and b are declared as having the type unsigned char变量ab被声明为具有unsigned char类型

unsigned char a,b;

If you want to enter integers in these variables you need to write如果你想在这些变量中输入整数,你需要写

scanf("%hhu",&a);

and

scanf("%hhu",&b);

So your program will look like所以你的程序看起来像

#include <stdio.h>

int main( void )
{
    unsigned char a, b;

    printf( "Enter first number : " );
    scanf( "%hhu", &a );

    printf( "Enter 2nd number : " );
    scanf( "%hhu", &b );

    printf( "a&b = %d\n", a & b );

    printf( "a|b = %d\n", a | b );

    printf( "a^b = %d\n", a ^ b );

    printf( "~a = %d\n", a = ~a );

    printf( "b<<1 = %d\n", b << 1 );

    printf( "b>>1 = %d\n", b >> 1 );
}

The program output might look like程序 output 可能看起来像

Enter first number : 10
Enter 2nd number : 15
a&b = 10
a|b = 15
a^b = 5
~a = 245
b<<1 = 30
b>>1 = 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM