#### Archived

This topic is now archived and is closed to further replies.

# Bitshifts and other op's

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK, one thing i''v never looked into much are the the following signs, and was wondering if anyone knows any tutorials explaining in detail how they are used in programming (possiblly C#): ^ | & << >> <> Now I know a little about each (except <> which i''m not sure is one), but I would like a good tutorial explaining them in more detail.

##### Share on other sites
Any c#/c/c++ reference will do.

^ = XOR
| = OR
& = AND
<< = left bit shift
>> = right bit shift

In some other languages (vb for example), <> is considered the NOT operator.
However, in c-type languages, the exclamation mark (!) represents it.

[edited by - Nik02 on August 7, 2003 6:05:36 AM]

##### Share on other sites
<< can also be the "insertion operator"
>> can also be the "extraction operator"

when dealing with C++ style streams...

as for the binary ops..i can give a few examples to get you started:

01010101 and 00001111 = 00000101 (useful in bitmasking)
01010101 or 00001111 = 01011111 (useful in creating flags)
01010101 xor 00001111 = 01011010 (hmmm...simple encryption)

bitshifting works as follows ( these are in binary for the sake of clarity, so if you entered this into your lovely compiler it would assume decimal and fux0r )

00001111 << 2 = 00111100 ( bits are shifted 2 places to the right, anything that moves beyond the variables border is lost )

>> works the same but shifts to the left

##### Share on other sites
Tnx for the replies. I understand the << >> <> | and & now, but still arnt too sure about the ^, I use it for things like enabling/disabling buttons with button.Enabled^=true but I dont know how it works. I'm just skimming through that page now, but if it isnt on there can someone help with that, if it is just say and i'll find it l8r.

Tnx for the help

PS OH and one thing u could maybe help me on is, im making a button with colors, and when its disabled I want to created a faded version of that color. I tryed the built in FromArgb but it doesnt work. So heres what I need, an alpha value, IE the value I want the color of the button to fade into the background. I have the color of the button, the alpha value (IE 50 = 50% opacity) and the back color. What can I do to the RGB values to get it to act like a faded color. I'm just playing about with it now, with some pretty weird effects

[edited by - malpass on August 7, 2003 6:44:15 AM]

##### Share on other sites
quote:
Original post by Nik02
In some other languages (vb for example), <> is considered the NOT operator.
However, in c-type languages, the exclamation mark (!) represents it.

[edited by - Nik02 on August 7, 2003 6:05:36 AM]

That is incorrect... <> in VB means less than or greater than. The word NOT is considered the NOT operator . By meaning, if you check a variable for being less than a value or greater than a value, it logically means anything but that value. It is not a replacement for the NOT operator however.

The XOR operator ^

XOR means exclusive or, so...
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0

So, when you''re using it for changing the status of a variable..

Say you have a boolean flag, values can only be 0 or 1 (simple example):
SomeFlag = 0; //Start with flag off//Change status of the flagSomeFlag^=1; //0 ^ 1 = 1, so it went from 0 -> 1 (off to on)//Lets change it again:SomeFlag^=1; //1 ^ 1 = 0, so it went from 1 -> (on to off)

Now you can see why your variable is acting the way it is . you can even use XOR to swap 2 variables without using a temporary .

v1 = 1; //01v2 = 3; //11v1 ^= v2; //01 ^ 11 = 10v2 ^= v1; //11 ^ 10 = 01v1 ^= v2; //10 ^ 01 = 11

As you can see, v1 started as 1, and ended as 3, while v2 started as 3 and ended at 1. Just some info to show you how neat bitwise operators can be. I have used the pretty extensively in my programming.

##### Share on other sites
Cool, thx for that.

I think iv done the color thingy:

For each, R, G and B, do this

BackColor.R+(TopColor.R-BackColor.R*percent)

where percent = 0-1

This looks right when I test it but could anyone confirm?

##### Share on other sites
quote:

That is incorrect... <> in VB means less than or greater than. The word NOT is considered the NOT operator . By meaning, if you check a variable for being less than a value or greater than a value, it logically means anything but that value. It is not a replacement for the NOT operator however.

Agreed.
However, for value comparisons, it works exactly same as the real NOT.
I didn''t put much significance in VB''s syntax on my post, anyway...

rgds Nik

##### Share on other sites
quote:

>> works the same but shifts to the left

Shouldn''t that be right?

##### Share on other sites
quote:
Original post by malpass
Cool, thx for that.

I think iv done the color thingy:

For each, R, G and B, do this

BackColor.R+(TopColor.R-BackColor.R*percent)

where percent = 0-1

This looks right when I test it but could anyone confirm?

I'm thinking you want something more like this for blending two colors:

ipercent = 1.0f-percent;
//Assuming you wanted percent to mean the % of topcolor to show
value.R = BackColor.R*ipercent + TopColor.R*percent;
value.G = BackColor.G*ipercent + TopColor.G*percent;
value.B = BackColor.B*ipercent + TopColor.B*percent;

--- Edit ---
Just wanted to ask exactly what you're trying to accomplish with your colors

[edited by - Ready4Dis on August 7, 2003 5:14:52 PM]

• 10
• 17
• 9
• 14
• 41