473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bitshifting independant of signedness


Just wondering if bitwise operators pay any attention to the signedness of
an integer type? Do they pay any attention to, or work any differently
with, the sign-bit, or with a signed integer type?

Let's take a hypothetical system where:

(1) int is comprised of 32 value bits (inclusive of the sign-bit).
(2) Two's complement is used.

The bit-pattern for -18 on such a system is:

1111 1111 1111 1111 1111 1111 1110 1110

On such a system is the following *guaranteed* to work?

#define VALBITS_INT 31
/* The above figure does NOT
inlude the sign bit. */

int Minus18()
{
int i = 0;

/* (1) First set the sign-bit: */

i |= 1U << VALBITS_INT;
/* Will this definitely set the sign-bit?
Is the U tag necessary on the literal? */

/* (2) Set all other bits to 1: */

int const MSBonly = 1 << VALBITS_INT-1;

i |= MSBonly | MSBonly-1;

/* (3) Toggle first and fifth bits */

i ^= 1;
i ^= 16;

return i;
}

When performing a bitwise operation, are the integer types treated as if
they're unsigned?

--

Frederick Gotham
Aug 24 '06 #1
4 1489
Frederick Gotham schrieb:
Just wondering if bitwise operators pay any attention to the signedness of
an integer type? Do they pay any attention to, or work any differently
with, the sign-bit, or with a signed integer type?

Let's take a hypothetical system where:

(1) int is comprised of 32 value bits (inclusive of the sign-bit).
(2) Two's complement is used.
(3) You assume that there are no padding bits in signed int
that are not also there in unsigned int.
(4) You assume that INT_MAX+1U != 0 (the "and vice versa"
part for (3))
The bit-pattern for -18 on such a system is:

1111 1111 1111 1111 1111 1111 1110 1110

On such a system is the following *guaranteed* to work?

#define VALBITS_INT 31
/* The above figure does NOT
inlude the sign bit. */

int Minus18()
{
int i = 0;

/* (1) First set the sign-bit: */

i |= 1U << VALBITS_INT;
/* Will this definitely set the sign-bit?
Is the U tag necessary on the literal? */
Depends. The 1U will be detrimental if (4) does not hold as
you would then shift "too far".
>
/* (2) Set all other bits to 1: */

int const MSBonly = 1 << VALBITS_INT-1;

i |= MSBonly | MSBonly-1;
Will not set all bits if (3) does not hold.
i = ~0;
certainly works better in that respect.
>
/* (3) Toggle first and fifth bits */

i ^= 1;
i ^= 16;

return i;
}

When performing a bitwise operation, are the integer types treated as if
they're unsigned?
No. Note that you could "assemble" the representation and then
reinterpret it:
int i;
unsigned int u = -1;
unsigned neg = 18;
u ^= (neg - 1U);
i = *(int *)&u;
This needs (1) to (4) as well.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 24 '06 #2
Frederick Gotham wrote:
>
Just wondering if bitwise operators pay any attention to the signedness of
an integer type? Do they pay any attention to, or work any differently
with, the sign-bit, or with a signed integer type?

Let's take a hypothetical system where:

(1) int is comprised of 32 value bits (inclusive of the sign-bit).
(2) Two's complement is used.

The bit-pattern for -18 on such a system is:

1111 1111 1111 1111 1111 1111 1110 1110

On such a system is the following *guaranteed* to work?

#define VALBITS_INT 31
/* The above figure does NOT
inlude the sign bit. */

int Minus18()
{
int i = 0;

/* (1) First set the sign-bit: */

i |= 1U << VALBITS_INT;
/* Will this definitely set the sign-bit?
Is the U tag necessary on the literal? */
I think your attempt is undefined.
>
/* (2) Set all other bits to 1: */

int const MSBonly = 1 << VALBITS_INT-1;

i |= MSBonly | MSBonly-1;

/* (3) Toggle first and fifth bits */

i ^= 1;
i ^= 16;

return i;
}

When performing a bitwise operation,
are the integer types treated as if
they're unsigned?
No.

Setting the sign bit with a left shift, looks undefined to me.

N869
6.5.7 Bitwise shift operators

[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1×2E2, reduced
modulo one more than the maximum value representable in the
result type. If E1 has a signed type and nonnegative value,
and E1×2E2 is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.

--
pete
Aug 24 '06 #3
Michael Mair posted:
(3) You assume that there are no padding bits in signed int
that are not also there in unsigned int.
I said it had 32 value bits, not 32 object bits.

--

Frederick Gotham
Aug 25 '06 #4
Frederick Gotham schrieb:
Michael Mair posted:
> (3) You assume that there are no padding bits in signed int
that are not also there in unsigned int.

I said it had 32 value bits, not 32 object bits.
Yes. However, only the common range of unsigned int and signed
int is guaranteed to have the same representation.
I do not find anything in the standard against either
signed: sign bit -- four padding bits -- 31 value bits
unsigned: five padding bits -- 31 value bits
or
signed: sign bit -- four padding bits -- 31 value bits
unsigned: four padding bits -- 32 value bits
or
signed: sign bit -- four padding bits -- 31 value bits
unsigned: 36 value bits
or, what you were hoping for
signed: sign bit -- four padding bits -- 31 value bits
unsigned: one value bit -- four padding bits -- 31 value bits
where the above specifies the "bit order" of the representation.
So, without the stipulation that unsigned has one more value
bit than signed int and that that additional value bit is
"in the same place" as the sign bit of the signed int, your
expectations may be off.
You may shift too far for identical numbers of value bits in
the first of the above cases. Or you may fail to set the "sign
bit" in the second case.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 25 '06 #5

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

Similar topics

0
by: Tarek Ziadé | last post by:
Hi list, I am trying to code some utilities to bench code performances, machine-independant, to be able to measure the absolute cost of a piece of code. So i came up with the pystone idea...
14
by: Champika Nirosh | last post by:
Hi All, We have a windows from application written in C#, there we have used Browser COM and other basic libraries present in standard .NET/C# SDK. So the next part is to make this appliaction...
2
by: tshad | last post by:
I have a page that I need to tell how many INDEPENDANT hits have occurred. I have the following code: Sub Page_Load(sender as Object, e as EventArgs) if not IsPostBack then Call...
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
0
by: Finn Stampe Mikkelsen | last post by:
Hi Is there an easy way of having seperate alignment for Column headers and the actual columns in each row... It would be great to have headers centered, but individual columns left or right...
16
by: Pietro Cerutti | last post by:
Hi group, is it always safe to pass unsigned char * variables as parameters to functions accepting char * arguments? For instance, I have to compare two unsigned char * strings. Can I safely...
2
by: sono | last post by:
1. I am looking which HTML CODE is AS GENERAL AS POSSIBLE (i.e. independant of platform and browser). 2. To be more explicit : 2.a) I am writing HTML code which I would like to run on ALL...
10
by: Alef Veld | last post by:
Hi everyone, I'm trying to get my head around the concept of bit shifting, and there are some things that seem to elude me. Many thanks for your answers. 1. When shifting bits, is there a...
3
by: tedmoseby | last post by:
I am creating a website, using flash instances which will be dynamicly based on xml information (pulled from, the users, sql db; thru php). I am running into a problem, mapping this out on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.