473,473 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Bit operations

Hi,
Please look at the following program:

int main(void)
{
char c;
c = -16;

if(c & (0x1<<7) !=0)
printf("TRUE\n");
else
printf("FALSE\n");

return 0;
}

The above program should print TRUE ( as -128 is not equal to 0) but
the above program is printing FALSE. Can anybody please tell why it is
so?

PS It is NOT a homework. I'm using the above concept in initializing
the bits for the Block Bitmap.

Dec 27 '05 #1
4 1378
alice wrote:
Hi,
Please look at the following program:

int main(void)
{
char c;
Whether `char' is signed or unsigned is system dependent (and a type
distinct from `unsigned char' or `signed char' as well). If you are
going to assign a negative number to it, it would be better to make it
explicitly signed.
c = -16;

if(c & (0x1<<7) !=0) ITYM:
if ((c & (0x1 << 7)) != 0)

[!= binds more tightly than &]
printf("TRUE\n");
else
printf("FALSE\n");

return 0;
}

The above program should print TRUE ( as -128 is not equal to 0) but
the above program is printing FALSE. Can anybody please tell why it is
so?


HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 27 '05 #2
In article <11**********************@f14g2000cwb.googlegroups .com>,
alice <al***********@yahoo.com> wrote:
Please look at the following program: int main(void)
{
char c;
c = -16;
In C, it is up to the compiler writers to decide whether a plain
"char" variable is "signed" or "unsigned".

If, on your implementation, "char" is signed, then then assignment will
store the -numeric- value you are expecting... but not necessarily the
bit pattern you are expecting.

If, on your implementation, "char" is unsigned, then you would
have an implicit signed-to-unsigned conversion going on. The value
stored would be UCHAR_MAX + 1 - 16; i.e., 240 for the common case
of UCHAR_MAX being 255.

if(c & (0x1<<7) !=0)
printf("TRUE\n");
else
printf("FALSE\n");

return 0;
} The above program should print TRUE ( as -128 is not equal to 0) but
the above program is printing FALSE. Can anybody please tell why it is
so?


You are counting on a particular bit representation for a negative
value, but C allows implementations to choose from three different
bit representation for negative values. The most common representation
is "twos complement" (-x is 1 plus the bitwise complement of x,
+0 and -0 are the same), but C also allows "ones complement"
(-x is the bitwise complement of x, +0 is "all bits clear", -0 is
"all bits set"), and "signed magnitude" (there is a bit which
does nothing other than hold the sign of the value, and -x has
the same value bits as x but with the sign bit changed.)

Notice that in "signed magnitude" representation in which
a total of 8 bits are stored per character, 1<<7 is outside
the representable positive range (which only uses 7 bits for value)
and so would overflow with undefined results
(most likely that 1<<7 would be 0.)

Notice that in 2s complement signed character representation in which 8
bits are stored per character, 1<<7 would try to represent +128 but
that +127 is the largest possible positive number in that
situation, so the behaviour would be undefined.
I notice that you have used 0x1<<7 instead of 1<<7 . That
suggest to me that you might be thinking that somehow using
the 0x notation indicates an unsigned value. That is not the
case, though: 0x notation merely indicates hexidecimal representation.
If you want to work at the bit level, you should not
*assume* that you are on a 2s complement machine (but it
might be fair to -test- to see if you are, and to abort
the program if you are not.)

You should never assume that char is signed or unsigned.
If you want signed or unsigned char, then code
"signed char" or "unsigned char" explicitly.

When you are working at the bit level, working with signed
values can lead to unexpected results unless you know exactly
what the rules are. If you want to work at the bit level, you
should probably only work with unsigned quantities.

Also, you should not assume that manipulation of literal constants
(such as 1<<7) is working with unsigned values. You should instead
take the care to use the appropriate suffix to indicate that you
want the unsigned version of the constant. For example, 1U << 7
--
All is vanity. -- Ecclesiastes
Dec 27 '05 #3
alice wrote:
Hi,
Please look at the following program:

int main(void)
{
char c;
c = -16;

if(c & (0x1<<7) !=0)
printf("TRUE\n");
else
printf("FALSE\n");

return 0;
}

The above program should print TRUE ( as -128 is not equal to 0) but
the above program is printing FALSE. Can anybody please tell why it is
so?

PS It is NOT a homework. I'm using the above concept in initializing
the bits for the Block Bitmap.

The expression you are testing is of type int. c is converted to int as

0xfffffff0 and or course 0x1 << 7 looks like..
0x00000080 and anding them results in..
0x00000080 which is != 0 and therefore FALSE.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 27 '05 #4

alice wrote:
Hi,
Please look at the following program:
[...]
if(c & (0x1<<7) !=0) The above program should print TRUE ( as -128 is not equal to 0) but


[...]

try this
/****/
if((c & (0x1<<7)) !=0)
/****/

precedence of != operator is higher.

Dec 28 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Lull | last post by:
I'm writing a multithreaded COM server to manage a pool of hardware resources. All objects are designed to be thread-safe, and I've set sys.coinit_flags to COINIT_MULTITHREADED before importing...
0
by: vikram.cvk | last post by:
Hello Experts, Im trying to design a CSS vertical drop down menu which should have the following functionality. Home About Us | -->Overview
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
7
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures,...
6
by: BCC | last post by:
I am noticing what seems to be a huge drop in performance in STL from VC6.0 to VC7.1. Particularly with vector. The following code shows what I mean... Any thoughts? Thanks, B
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
4
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
80
by: pereges | last post by:
Hello, I have the following structure - typedef struct { double x, y, z; }vector; In certain places, I could avoid triplification of code by using an array instead of x, y, z. For eg:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.