Home > T-SQL > Assignment & Bitwise Symbol – SQL Server

Assignment & Bitwise Symbol – SQL Server


= (Assignment operator)

It is used for assigning value to a variable

for instance:

Declare @Var as smallint = 20 — Assign a value to a variable at the time of declaration

print @var

Set @Var = 15 — Assigning a value to a variable

Print @var

Bit operators

Operators used to manipulate bit value of a variable. Bit operators are &, | and ^.

& (bit AND)

1 & 1 = 1

1 & 0 = 0

--Tinyint is 1 byte = 8 bit
Declare @VarA as Tinyint = 10,
	@VarB as Tinyint = 3

Print @VarA & @VarB

Binary calculation
00001010
00000011
--------
00000010
--------

| (Bit OR)

1 | 1 = 1

1 | 0 = 1

--Tinyint is 1 byte = 8 bit
Declare @VarA as Tinyint = 10,
	    @varB as Tinyint = 3

Print @VarA | @VarB

Binary Calculation
00001010
00000011
--------
00001011
--------

^ (Bit Exclusive OR)

1 ^ 0 = 1

0 ^ 0 = 0

1 ^ 1 = 0

--Tinyint is 1 byte = 8 bit
Declare @VarA as Tinyint = 10,
	    @varB as Tinyint = 3

Print @VarA ^ @VarB

00001010
00000011
--------
00001001
--------

~ (Bit NOT)

0 = 1

1 = 0

--Tinyint is 1 byte = 8 bit
Declare @VarA as Tinyint = 0

Print ~@VarA

00000000 = 0
--------
11111111 = 255
--------

We can also use assignment operators with this Bitwise operator to assign the value after calculation.

&=, |=, ^= (Compound operator)

Thanks for reading.

Advertisements
  1. lli
    November 9, 2011 at 11:57 am

    There are miskate1&1=0 but 1

  2. lli
    November 9, 2011 at 4:13 pm

    Sorry I typed my previous message with my phone

  3. November 9, 2011 at 4:59 pm

    No problem, I can read that 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: