• Post Reply Bookmark Topic Watch Topic
  • New Topic

The Wonders of Programming with Bits  RSS feed

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a new aspiring programmer, I've came to realize by curiosity that programming with bits is very interesting indeed. After reading certification guides that says the shift operators are of no relevance to today’s computing needs due to the increase in RAM storage that come with machines at present (hence not teaching the reader how to use them), I went ahead and did the opposite and came up with fascinating results.

A program that converts an integer to its binary form using the shift operator:



Maybe what these authors say is true, but from my experiments using these wonderful operators I think that they make great tools for efficient programming making programs compact and faster (my assumptions).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those assumptions don't hold up though.

I can write a much more compact program than the one you wrote:


Also readable code is more important than compact code. Google "Perl one liners" to see some unreadable (but compact) code.

Now for efficiency. I wrote a test harness to run your code (vs the built in one) a million times. Your code takes 480-520 milliseconds on my computer. The Java method takes 90-95 milliseconds on my computer.

Bit twiddling does have a use in specialized apps so no harm in learning it. But I agree that it doesn't belong in general use code. And omitting it from the certification guide makes sense. It just isn't something every Java developer needs to know.


 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne, you want one-liners? Try APL!

A free beer on their next visit to Loudoun county, Virginia, USA, to the first person who can tell me what this complete APL program does:



Bit-twiddling is probably no way (in Java, at least) to improve performance, but it has its uses. Somebody, after all, has to write the code that does those nifty animated LED sign displays.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I see. Guess there is a whole lot for me to learn when it comes to computers and programming. Thank you for the knowledge, I shall continue to seek wisdom.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoted for emphasis:
Jeanne Boyarsky wrote:readable code is more important than compact code.

In fact, I'm gonna quote it again with even more emphasis:
Jeanne Boyarsky wrote:readable code is more important than compact code.

This quote should be embroidered on a sampler and hung on every developer's wall.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:you want one-liners? Try APL!

APL: what happens when you invent a programming language while tripping on acid.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Jeanne, you want one-liners? Try APL!

A free beer on their next visit to Loudoun county, Virginia, USA, to the first person who can tell me what this complete APL program does:



Bit-twiddling is probably no way (in Java, at least) to improve performance, but it has its uses. Somebody, after all, has to write the code that does those nifty animated LED sign displays.


Hmm, that looks like an implementation of Conway's Game of Life to me.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Stevens Miller wrote:Jeanne, you want one-liners? Try APL!

A free beer on their next visit to Loudoun county, Virginia, USA, to the first person who can tell me what this complete APL program does:



Bit-twiddling is probably no way (in Java, at least) to improve performance, but it has its uses. Somebody, after all, has to write the code that does those nifty animated LED sign displays.


Hmm, that looks like an implementation of Conway's Game of Life to me.


See you at Clancy's!
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Quoted for emphasis:
Jeanne Boyarsky wrote:readable code is more important than compact code.

In fact, I'm gonna quote it again with even more emphasis:
Jeanne Boyarsky wrote:readable code is more important than compact code.

This quote should be embroidered on a sampler and hung on every developer's wall.


I understand your passion for readable code by your post... I'm religious about readable code myself, this was just a personal experiment for self research (no need to get fired up)
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Mike. J. Thompson wrote:
Stevens Miller wrote:Jeanne, you want one-liners? Try APL!

A free beer on their next visit to Loudoun county, Virginia, USA, to the first person who can tell me what this complete APL program does:



Bit-twiddling is probably no way (in Java, at least) to improve performance, but it has its uses. Somebody, after all, has to write the code that does those nifty animated LED sign displays.


Hmm, that looks like an implementation of Conway's Game of Life to me.


See you at Clancy's!


Ha, I'll be there. I'm thousands of miles away though so it could be some time!

The surprising thing is that that APL program actually makes (a little) more sense than you'd expect when you know what the symbols mean. Still not sure I'd want to work with a language like that every day though.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did work with APL every day in my younger days when I was a consultant. We connected to I. P. Sharp Associates over the telephone system (that being the only available network in those days) and wrote code for such things as school board budgeting.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Stevens Miller wrote:
See you at Clancy's!

Ha, I'll be there. I'm thousands of miles away though so it could be some time!

Just let me know. We have some local microbreweries that might impress you.

The surprising thing is that that APL program actually makes (a little) more sense than you'd expect when you know what the symbols mean.


That path leads to The Dark Side.

I put in a lot of wasted time during my college years on a Selectric-style dial-up terminal with one of those APL typeballs, in the late '70s. At the time, it seemed like it was a kind of mystic rune-set, known only to the high priests. When we got to the overstrike characters, though, I should have realized I was being had.

A few colleagues told me that a version of APL existed that used readable keywords in place of those insane glyphs. I've never seen such code, but maybe it could be maintained. The idea of actually relying on something like the above in a production environment, where no one knows who might be the next person who has to deal with it, fills me with dread, however.

You know, the IBM people were clever enough to retool their amazing Selectric typewriters into APL terminals, but we found out by trying that a regular Selectric type-ball does not have the familiar alphanumeric characters in the same places as the caps-only alphabetics and numerics are located on the APL type-ball. So, to get APL to do an early kind of word-processing, we had to write functions that took APL strings in, and produced Selectric alphanumeric type-ball equivalent characters out. At the time, it seemed like quite a respectable accomplishment. But then, so did punching love notes to my girlfriend into paper tape with 5x7 characters on an ASR-33.

Ah, those were the days...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:... the shift operators are of no relevance to today’s computing needs due to the increase in RAM storage that come with machines at present

That sounds really strange. Bit shift operators do not have anything to do with the amount of RAM that a computer has. Maybe you misunderstood what the guide was saying.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Rico Felix wrote:... the shift operators are of no relevance to today’s computing needs due to the increase in RAM storage that come with machines at present

That sounds really strange. Bit shift operators do not have anything to do with the amount of RAM that a computer has. Maybe you misunderstood what the guide was saying.


I can only guess that the guide was assuming that the only use of bit-shift operators would be to compact values into bits using masking and shifting.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Jesper de Jong wrote:
Rico Felix wrote:... the shift operators are of no relevance to today’s computing needs due to the increase in RAM storage that come with machines at present

That sounds really strange. Bit shift operators do not have anything to do with the amount of RAM that a computer has. Maybe you misunderstood what the guide was saying.


I can only guess that the guide was assuming that the only use of bit-shift operators would be to compact values into bits using masking and shifting.


I actually think that the argument that bit shift operators are of less relevance is kinda valid, but *not* for the argument of RAM that was given. I still find it used a lot when the network is used, as it is needed in serialization and deserialization. It is also used in the network protocol itself such as the headers, etc. It is also still used in graphics.

I think the issue are the frameworks. You can find a library or framework for everything these days. Heck, even Java itself has a ridiculous amount of a core library, when compared to what is available in a core C library. Every example that I named have a library that supports it -- and it is kinda silly to not use a library for a network protocol or graphics package.


So, in other words, the shift operators still have lots of relevance, but to a newbie programmer, or even an experienced programmers coding with high-level APIs, it can be argued that it has less relevance.

Henry
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mike. J. Thompson wrote:
Jesper de Jong wrote:
Rico Felix wrote:... the shift operators are of no relevance to today’s computing needs due to the increase in RAM storage that come with machines at present

That sounds really strange. Bit shift operators do not have anything to do with the amount of RAM that a computer has. Maybe you misunderstood what the guide was saying.


I can only guess that the guide was assuming that the only use of bit-shift operators would be to compact values into bits using masking and shifting.


I actually think that the argument that bit shift operators are of less relevance is kinda valid, but *not* for the argument of RAM that was given. I still find it used a lot when the network is used, as it is needed in serialization and deserialization. It is also used in the network protocol itself such as the headers, etc. It is also still used in graphics.

I think the issue are the frameworks. You can find a library or framework for everything these days. Heck, even Java itself has a ridiculous amount of a core library, when compared to what is available in a core C library. Every example that I named have a library that supports it -- and it is kinda silly to not use a library for a network protocol or graphics package.


So, in other words, the shift operators still have lots of relevance, but to a newbie programmer, or even an experienced programmers coding with high-level APIs, it can be argued that it has less relevance.

Henry


I certainly agree that it has limited relevance in a high level language like Java, and it's much more important that newbies learn a solid understanding of how to write maintainable OO code. I certainly wouldn't ever try and write my own low level network protocol API for example (I've used things like Thrift and Google Protocol Buffers for that etc). However if I ever have a very specific and isolated need it's sometimes quicker to write and test a small method than to search for a library to do it. For example (and this is just a completely hypothetical example), if I had an array of bytes that represented unsigned shorts and I didn't know off the top of my head what API was available to extract the values it might be quicker for me to just write a method to do it than to search for and download the library.

Plus it's nice to have a chance at understanding code that does use them on the rare occasions that I come across it.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico,

thanks for bringing this topic. It brought back memories. In the very early 90's I had an Archimedes computer that came with an ARM3
processor (a predecesor of todays chips in the mobile devices). That processor was a Risc-processor, and it only had 27 instructions.
But these instructions were general and powerfull, and gave the advantage of quickly learning assembler.

Now, this processor did not have any floating point unit, but it had an integer multiply instruction, that could multiply
the contents of two registers and store it in another one. However, such an instruction could easily take well over
10 clock cycles. It also had, what was called', a 'magic barrel shifter', that let you do bit shifting of a register
without any time penalty.

So you had the instruction: Add Rd, Ra, Rb, ASL x

What this does is: shift the contents of register Rb x places to the left (arithmetically), in fact multiplying it with 2^x,
add the result to register Ra and store the result in register Rd. All this in just one clock cycle.

If you had to multiply a register by, say, nine, you would simply use

Add Ra, Ra, Ra, ASR #3

and, as siad, just one clock cycle.

You could also substract the first register from the second, like in:

RSB Rd, Ra, Ra, ASL #3

This gave Ra * 2^3 - Ra = 7*Ra, stored in register Rd.

Now, if you wanted to multiply by 14, you could do that in two instructions:

RSB Ra, Ra, Ra, ARL #3
Add Ra, Ra, Ra

Two clock cycles. It then became a sport to find out how to calculate a multiplication
in the shortest possible number of instructions. Those were the days...

And as far as APL is concerned: I've never seen a languange that is more powefull
than APL (also speed wise). In the early 90's we built a reporting system for an
insurance company, consistng of APL for the calculations and SAS for the data.
As I said, this APL was truely amazing. You could do things using half a line of
code that would take 10K of Java code. But we ended up in a sort of competition:
if I managed to do something in 5 glyphs, then next day my collegue showed how
it could be done in 3 glyphs. Indeed, maintainability was the least of our concerns...
and we got heavily punished for that.

To end these memories of an aging person: I certainly think that bit juggling is hardly
seen nowadays, partly because of ever increasing RAM sizes.
When writing a chess program on my Archimedes, I only had 4Mb of RAM.
So I stored a move in 4 bytes: first six bits the from field, 6 bits for the to field,
3 bits for the captured piece, 1 bit for en passant, et cetera.
Now, in Java, I have a Field class, a Move class, with a boolean for ep, a Field object
for the from field, et cetera. No more bit juggling.

Greetz,
Piet
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... with a few more years of dedication and persistence I'll be as experienced and knowledgeable as you'll to tell such stories
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!