473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitwise Operator

Hello All,

I was trying to learn bitwise operator in C .In that process I cam
across one resource as mentioned bellow
http://graphics.stanford.edu/~seande...rationCounting

I have one question regarding the section

"Compute the minimum (min) or maximum (max) of two integers without
branching"
In this section author is indicating "if x < y, then -(x < y) will be
all ones" .

My understanding is if negative number is represented in two's
complement method then the above statement is true not for other
representation .

And C standard does not restrict that negative number should be
represented in 2's complement method.

Is my understanding correct?
Please provide some inputs.
Regards,
Somenath

Oct 5 '07 #1
1 1804
somenath:
I was trying to learn bitwise operator in C .In that process I cam
across one resource as mentioned bellowhttp://graphics.stanfo rd.edu/~seander/bithacks.html#O perationCountin g

WARNING: The site assumes in about 50 thousand places that sizeof(int)
yields the amount of value bits... IT DOESN'T.

I've taken a look at the site and it looks quite strange. For
instance, if I want to know the sign of a value, I'll just do:

bool is_neg = value < 0;

As you can see, no need for a load of crap.>

In this section author is indicating "if x < y, then -(x < y) will be
all ones" .

Non-portable assumption that every system is two's.

My understanding is if negative number is represented in two's
complement method then the above statement is true not for other
representation .

Correct.

And C standard does not restrict that negative number should be
represented in 2's complement method.

Again correct.

Is my understanding correct?

Yup.

Martin

Oct 5 '07 #2

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

Similar topics

9
2534
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask of 'flags' for users on a website. The function below is supposed to be a search function for one of those flags in a particular thing. The idea is that when something calls userId(), it should get to see what type of user flags that are...
12
18624
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
2
3468
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
9
7030
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
5
13431
by: Bill Dee | last post by:
I need help converting a tiny piece of code which uses the bitwise complement operator from C# to VB.NET. In C# I currently have this: long useThis = Myclass.ALLCONSTANTS; long doNotUse = Mycalls.ABC | Myclass.XYX; long useThis &= ~doNotUse; bitwise removal of flags not to use How do I convert this to VB.NET? I know to use "OR" instead of " | "
10
2178
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
3
13151
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should work but doesn't: byteArray &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0) The error is "Constant value '-225' cannot be converted to a 'byte'".
17
2365
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they make use of a MODE flag that gets passed in. They then compare the mode flag against a hard coded value using bitwise AND, and then show or don't show certain features based on the mode. Example pseudocode: if (mode & 1) do something if (mode...
5
3295
by: Junmin H. | last post by:
Hello, all. I never use them when I am programming. But i have been reading codes that using them. When should I use them? Why not use char/short/int/long? Is it very important to know them well? Because it's binary, I found that it's a little difficult to understand then decimal. Thanks. Junmin H.
8
8792
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well. BRgds, Daniel.
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.