By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

bitwise increment one liner

P: n/a
The only adder I was able to come up with for incrementing
by one, uses a for loop.

I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)

Anyone know of a way to increment by one continuously,
while updating the same value stored in an object,
without the need of such a loop? basically a one-liner.

--
nethlek
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Mantorok Redgormor wrote:
The only adder I was able to come up with for incrementing
by one, uses a for loop.

I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)

Anyone know of a way to increment by one continuously,
while updating the same value stored in an object,
without the need of such a loop? basically a one-liner.


I don't understand your question at all. You may need to clarify in
order to receive help.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

P: n/a
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<L3***************@newsread2.news.pas.earthli nk.net>...
Mantorok Redgormor wrote:
The only adder I was able to come up with for incrementing
by one, uses a for loop.

I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)

Anyone know of a way to increment by one continuously,
while updating the same value stored in an object,
without the need of such a loop? basically a one-liner.


I don't understand your question at all. You may need to clarify in
order to receive help.

-Kevin


Sorry, I'll re-explain.

I have a bitwise adder, that is, addition done with bitwise operators
to continuously add one while in a for loop
However, the adder itself is made up of a for loop.

So I have something like:

for(initialization; condition; for(my adder))

That inner for loop there just continues to add one
so currently it is an obfuscated way of doing the following:
++i;

I was wondering if anyone knew a way to emulate
++i;
Without the need of a loop, where it can be done by just a one liner?
Or possibly two/three liner minus the need for a loop


--
nethlek
Nov 14 '05 #3

P: n/a
Mantorok Redgormor wrote:

Sorry, I'll re-explain.

I have a bitwise adder, that is, addition done with bitwise operators
to continuously add one while in a for loop
However, the adder itself is made up of a for loop.

So I have something like:

for(initialization; condition; for(my adder))

That inner for loop there just continues to add one
so currently it is an obfuscated way of doing the following:
++i;

I was wondering if anyone knew a way to emulate
++i;
Without the need of a loop, where it can be done by just a one liner?
Or possibly two/three liner minus the need for a loop


So you are seeking a short (loop-free), but obfuscated method of
incrementing a variable?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4

P: n/a
ne*****@tokyo.com (Mantorok Redgormor) writes:
I was wondering if anyone knew a way to emulate
++i;
Without the need of a loop, where it can be done by just a one liner?
Or possibly two/three liner minus the need for a loop


Perhaps
i ^= (i & ~-~i) | (~i & -~i);
Works for me for small "unsigned" i, at least. For greatest
obscurity, it could be written as
i^=i&~-~i|~i&-~i;
This is decipherable in a few minutes with _Hacker's Delight_ by
Henry Warren, which I used to construct it, but probably baffling
otherwise.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Nov 14 '05 #5

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
ne*****@tokyo.com (Mantorok Redgormor) writes:
I was wondering if anyone knew a way to emulate
++i;
Without the need of a loop, where it can be done by just a one liner?
Or possibly two/three liner minus the need for a loop


Perhaps
i ^= (i & ~-~i) | (~i & -~i);
Works for me for small "unsigned" i, at least. For greatest
obscurity, it could be written as
i^=i&~-~i|~i&-~i;
This is decipherable in a few minutes with _Hacker's Delight_ by
Henry Warren, which I used to construct it, but probably baffling
otherwise.


is the latter portable across sign-magnitude, ones' complement,
and two's complement?

also thanks for the reference to that book, I checked it out on
amazon going to buy it.

--
nethlek
Nov 14 '05 #6

P: n/a
ne*****@tokyo.com (Mantorok Redgormor) writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
ne*****@tokyo.com (Mantorok Redgormor) writes:
I was wondering if anyone knew a way to emulate
++i;
Without the need of a loop, where it can be done by just a one liner?
Or possibly two/three liner minus the need for a loop


Perhaps
i ^= (i & ~-~i) | (~i & -~i);
Works for me for small "unsigned" i, at least. For greatest
obscurity, it could be written as
i^=i&~-~i|~i&-~i;
This is decipherable in a few minutes with _Hacker's Delight_ by
Henry Warren, which I used to construct it, but probably baffling
otherwise.


is the latter portable across sign-magnitude, ones' complement,
and two's complement?


Unsigned values don't have any of those forms. If you want to
use it for signed values, I suspect it will work on typical two's
complement systems, but not on sign-magnitude or ones' complement
systems.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 14 '05 #7

P: n/a
ne*****@tokyo.com (Mantorok Redgormor) wrote in message news:<41**************************@posting.google. com>...
The only adder I was able to come up with for incrementing
by one, uses a for loop.

I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)


If you're just trying to count, say, 99 times, you don't need
to count 0,1,2,3,4,... but can use any sequence with a sufficiently
long period. In that case, an "increment" like
i += i | i < 0
should work, with an appropriate starting value for i.
(This particular example produces non-portable code.)
That should improve obfuscation!

Such an obfuscated counter can be implemented with fewer transistors
than an ordinary counter. I dimly recall finding such a counter in
early MacIntosh while disassembling some of its floppy-disk access code.

James
Nov 14 '05 #8

P: n/a
Groovy hepcat Mantorok Redgormor was jivin' on 17 Dec 2003 07:52:21
-0800 in comp.lang.c.
bitwise increment one liner's a cool scene! Dig it!
The only adder I was able to come up with for incrementing
You came up with a venomous reptile?
by one, uses a for loop.

I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)

Anyone know of a way to increment by one continuously,
while updating the same value stored in an object,
without the need of such a loop? basically a one-liner.


Well, since it's for obfuscated code... The following adds 19 to x.

int x=23,skedoo;
x:skedoo=0;if(x++,19>skedoo++)goto x;

Note the extra obfuscation achieved by using the same identifier for
both the label and the object being incremented as well as the use of
the comma operator.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.