Bitwise arithmetic. What's different between x <<= y and x << y ?

C# Bitwise arithmetic. What's different between  x <<= y and x << y ?

x <<= y  is evaluated as x = x << y (msdn)

well....

x = 69
y = 4

1) z = x << y      //  z = 1104

2) x <<= y         // x = 80

Why z != x ?
Somebody explain me please...
Dmitriy GalankinDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
Can't confirm thqt. In my test both functions return 1104, expample 1 in z, example 2 in x.
What variable type are x,y and z? In my test I've used int.
Dmitriy GalankinDeveloperAuthor Commented:
Type of variables:
x is byte
y is int
z is short
Dmitriy GalankinDeveloperAuthor Commented:
well... i need to get a ip header length from IPHeader
first byte oh header consist from version and header length
4 bits for version
4 bits for header length

byte versionAndIHL = 69
short version = (short)(versionAndIHL >> 4)   // return 4 - it's ok
short IHL = (short)(versionAndIHL << 4) // return 1104, but must be 20 (20 is length of ip header)
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Michael PfisterCommented:
Use the bitwise "and" operator:
IHL =  (versionAndIHL & 0xF)  * 4;

HTH

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
x = 69 means x = 64 + 4 + 1 = 2^6 + 2^2 + 2^0

hence the bit 6 and bit 2 and bit are 1 and all other bits are 0.

if you shift all bits 4 to the left the bit 6 was not move to bit 10 as a byte only has 8 bits. so you get bit 6 set to 1 and bit 4 set to 1 what is 64 + 16 == 80

Sara
sarabandeCommented:
4 bits for header length

I made a mistake. 1010 is 8 + 2 what is 10.

4 bits for the length would mean that the length has a range of 0 to 15.

so it couldn't be 20 what means that header length must be more than 4 bits.

i still would think that the header is big-endian what means that the order of the bits goes from left to right.

we should have at least a 16 bit integer (a short) to convert from network order (big-endian) to to host order (little-endian).

unsigned short usheaderBE = ...;    // extract a 16-bit integer from header
unsigned short usheaderLE = ntohs(usheaderBE);
 
byte IHL = usheaderLE & 0xFF;   // now extract lower 8 bits

Sara
sarabandeCommented:
can you post the structure definition that defines the header?

Sara
sarabandeCommented:
i found a comment in a source code that explains why the header length is times 4 of the half-byte value.

 * The IP header size, in bytes, is the value of the IP header length,
 * as extracted from the "ip_vhl" field of "struct sniff_ip" with
 * the "IP_HL()" macro, times 4 ("times 4" because it's in units of
 * 4-byte words
).  If that value is less than 20 - i.e., if the value
 * extracted with "IP_HL()" is less than 5 - you have a malformed
 * IP datagram.

that means the IHL counts the length in 32-bit (4-byte) integers.

Sara
sarabandeCommented:
The solution was to extract the byte from header and multiply it with 4 as the length stored in the byte counts 32-bit integers.

Sara
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.