Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bitwise Shift in C and Arithmetic Shift

Posted on 2003-03-22
6
Medium Priority
?
493 Views
Last Modified: 2008-02-26
If x is a singed char.  Does x >> n still the same as the integer division of 2^n and x << n same as the multiplication of 2^n?

Why does it work/not work?

How does arithmetic shift work?
Let's say if I have a signed char of 1001 in binary, -1 in decimal.
With an arithmetic right shift by 1 become 1100?  Would it means -4 in decimal?  so the multiplication we mentioned above won't work even for arithmetic shift.

Thanks!
0
Comment
Question by:JQLT3E
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8188318
8 bit signed char
x = 10000001 in binary, -1 in decimal.
x << 1 = 00000010 = 2 in decimal
x >> 1 = 01000000 = 64 in decimal

So yeah, they don't work for negative number.
For positive number, they work

y = 00010000 in binary, 16 in decimal.
y << 1 = 00100000 = 32 in decimal
y >> 1 = 00001000 = 16 in decimal

until ...
y << 4 = 1000000 = ... you know how many it is, don't you





0
 
LVL 2

Accepted Solution

by:
honey_hamster earned 200 total points
ID: 8188594
Signed chars are typically "two's complement", so the binary 8-bit value 10000001 is NOT -1.  -1 is represented by 11111111.

A series of "arithmetic right shifts" of positive numbers will eventually yield a result of 0 (00000000), whereas the same series of shifts on a negative result will eventually give the result of -1 (11111111).  In other words, division by shifting  right results in 'rounding down' on an absolute basis for positive values, and 'rounding up' on an absolute basis for negative values.

Some examples to show that right shifts work as division for negative numbers:

-16 / 2 = -8       11110000 >> 2 = 11111000 = -8

-59 / 16 = -4      11000101 >> 5 = 11111100 = -4
(Note the rounding up)

Hope this helps.
0
 
LVL 1

Expert Comment

by:TriG
ID: 8266815

You're right honey_hamster, but you're shifting by 1 too many in your examples.

/2  --->  >> 1
/16  --->  >> 4

--tri
0
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8269616
Thanks TriG - I shifted the binary number the correct number, but for some reason I put the wrong number after the ">>".  Oops.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10189594
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by honey_hamster.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question