473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

TO find whether MSB of a unsigned int/char is ZERO or ONE

Sir please consider the following code.I've tried the following code to print whether the MSB of a int/char is zero or one.
int main ()
{
unsigned char ch;
int i=0;
printf("enter the value\n");
scanf("%d",&ch);
if((ch & 10000000)==1)
printf("msb is one\n");
else
printf("msb is zero\n");
}
To the above code I've given input as 254(which has msb as one)but the output is displaying as msb is zero. Sir could you tell me what is wrong in the above code.Should I use "for loop" .
Thank you.
Jan 22 '07 #1
3 2078
horace1
1,510 Expert 1GB
try changing
Expand|Select|Wrap|Line Numbers
  1. if((ch & 10000000)==1)
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. if((ch & 10000000)==10000000)
  2.  
i.e. the logical AND of ch and 10000000 will be 10000000 if the MSB of the byte is set otherwise 0
Jan 22 '07 #2
willakawill
1,646 1GB
Sir please consider the following code.I've tried the following code to print whether the MSB of a int/char is zero or one.
int main ()
{
unsigned char ch;
int i=0;
printf("enter the value\n");
scanf("%d",&ch);
if((ch & 10000000)==1)
printf("msb is one\n");
else
printf("msb is zero\n");
}
To the above code I've given input as 254(which has msb as one)but the output is displaying as msb is zero. Sir could you tell me what is wrong in the above code.Should I use "for loop" .
Thank you.
Hi This expression
ch & 10000000
will return 10000000 or 128 decimal, not 1.

Try this
if ((ch / 128) == 1)
Jan 22 '07 #3
willakawill
1,646 1GB
try changing
Expand|Select|Wrap|Line Numbers
  1. if((ch & 10000000)==1)
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. if((ch & 10000000)==10000000)
  2.  
i.e. the logical AND of ch and 10000000 will be 10000000 if the MSB of the byte is set otherwise 0
This expression:
Expand|Select|Wrap|Line Numbers
  1. if((ch & 10000000)==10000000)
  2.  
will not work because '==' is not a bitwise operator. It is not required anyway because we are testing for true or false. Therefore
Expand|Select|Wrap|Line Numbers
  1. if(ch & 10000000)
  2.  
is either true or false. If true then MSB is 1
Jan 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
6
by: Sona | last post by:
Hi, What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following: char var; var = 0x00; what should var hold? and if it was an...
10
by: Angus Comber | last post by:
Hello My code below opens a Word document in binary mode and places the data into a buffer. I then want to search this buffer for a string. I tried using strstr but think it stops looking when...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
4
by: cdrom205 | last post by:
static void MDString ( unsigned char *input) { MD5_CTX context; unsigned char digest; unsigned int len = sizeof(input);//strlen (const char*) md5.MD5Init (&context); md5.MD5Update...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
10
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if(( (a >='0') && (a <='9')) | |...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
4
by: HackerisNewKnight | last post by:
Hello, 1) First problem. In my project i need to convert from char* to BYTE*( BYTE is unsigned char), is there any way to make the convertion? BYTE* bite; char* ch="help me, please"; bite =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.