[OT] Bit twiddling homework

Fri Jul 20 11:56:41 EDT 2018

```On Fri, 2018-07-20 at 10:38 -0400, Dennis Lee Bieber wrote:
> On Fri, 20 Jul 2018 11:00:09 +0200, Brian Oney via Python-list
> <python-list at python.org> declaimed the following:
>
> > Are 16|1 and 16+1 internally the same operation (for integers)?
>
> 	For those integers the EFFECT/RESULT will be the same. But...
>
> > > > 17 + 1
>
> 18
> > > > 17 | 1
>
> 17
Of course. There was a reason I chose those integers and timed them. I am ignorant of how Python stores an integer internally. Either way (2^x bit) the result would be the same regardless.
In retrospect, I seem to suggest that Python would have different sets of operations for an integer based on it's value; that's poor wording. Anyways.

> {From the original post}
> > For example, bitwise operators are neat (here a shift):
> > C:
> >  int res = 1 << 4
> >  printf("%d\n", res)
> > 16
> >
> > Translates to pseudocrap:
> > 0000 0001 -> leftwards bitshift of 4 places -> 0001 0000
> >
>
> 	Python also supports shift...
>
> > > > 17 << 2
>
> 68
> > > > 17 >> 2
>
> 4
Nice! Once again Python shines as a prototyping language!
> [Side comment, even in C bitwise operators aren't really considered playing
> "directly with memory". That would be more something like:
>
> 	int * i;			// i is a pointer (address) to an integer
> 	i = 0x01FF;		// set i to access address 511
> 	printf("%d\n", *i);	// print (as integer) whatever is stored at 511
>
> THAT you can not do in Python (at least, not without using something like
> the ctypes library).]
>
Thank you for the explanation. That is a nice example.
> 	Hex is basically just a means of shortening a binary string by
> representing runs of 4 bits using a single symbol. Same with Octal (runs of
> 3 bits). In both cases, each symbol translates to an integral number of
> bits, and position in a multi-symbol value can be translated directly.
>
> 	This is not possible when representing binary values in Decimal, as
> each place in the decimal representation does not map to an integral number
> of bits.
>
Yup! Wikipedia has the same info scattered about the hexadecimal page. You describe it nicely from the perspective of a computer scientist.
> > Therefore, what book or learning course do you recommend? I imagine something that tours or skims
> > the fundamentals of Boolean algebra and digital logic, and then goes to C and some fun homework
> > problems. It may seem to you that the emphasis there is wrongly placed.
>
> 	While Boolean algebra and digital logic are core concepts for
> computers, neither really appear in books that teach programming. Boolean
> logic is more in the realms of pure math and then feeds to digital logic
> (at the gate level -- NAND, NOR, AND, OR gates and combinations to create
> things like adders). Consider de Morgan's theorem:
>
> A & B	=>	not(not A | not B)
> A | B	=>	not(not A & not B)
>
> > > > b = 24
> > > > a = 19
> > > > a & b
>
> 16
> > > > a | b
>
> 27
> > > > a ^ b
>
> 11
> > > > ~(~a & ~b)
>
> 27
> > > > ~(~a | ~b)
>
> 16
Thank you for the advice. Those are also some nice examples. Cool, I can do this stuff in Python. I guess this is also why Python is so popular; an instructor can take one language and cover a ton of material without having to saddle students with learning another language.
> 	Can't really help with suggesting books -- The ones from my college
> books for such basic concepts. Even simple discrete 74xx chips are so rare
> these days that the idea of creating a four-bit ADDER from gates is hard to
> find.
>
> 	Parallax used to have a slew of educational stuff based on BASIC Stamps
> and a logic simulator (and project board), but neither appear to still be
> in their catalog (the books used to be available as PDF for most of their
> educational stuff). I did find a copy on a third-party site for the Digital
> Logic intro
> http://rambal.com/index.php?controller=attachment&id_attachment=686&usg=AOvVaw3YI9p_blIFAgardqgB2SBq
>
> Can't find a download for their logical simulator, but there is something
> that might be usable at
> https://sourceforge.net/projects/cedarlogic/
>
>
> 	Just to put this back on topic for the group...
>
> 	Pretty much all of what you ask can be learned in Python (and using the
> interactive interpreter is a lot faster than writing a C program, compiling
> it, etc. -- as can be seen by the cut&paste examples above). It is only
> direct hardware access that is not something Python is really suited for
> (although AdaFruit has a few boards which run CircuitPython
> [MicroPython+special libraries] if you really want to play with LEDs and
> such -- though they still don't access raw memory). If you really want
> hardware access -- as in the processor registers, memory, etc. -- you are
> probably talking about something like Arduino (AVR for Uno/Mega; ARM for
> Due)&look-alikes, TIVA-C Launchpads (ARM) [I don't have experience with the
> MSP430 Launchpads -- the MSP430 is much different beast from AVR and ARM
> processors], STM, AdaFruit (Metro are ARM&CircuitPython, though firmware
> can be replaced to work with Arduino IDE). Note that these are
> microcontrollers (the ARM versions use Cortex M-x processors) -- the
> BeagleBone Black and Raspberry-Pi, in contrast, are microcomputers (anyone
> remember when that term meant something like a TRS-80, Apple-II, Commodore
> PET <G>) running full OS, and hence making low level hardware access more
> difficult.
>
Thank you for all the information. Yes, I am tinkering mainly with Arduinos. You may have heard of MySensors; contributing there will give me a chance to learn, help out, and have some fun. The suggestion 'Hackers Delight' is very well suited.
Thanks again for the help! Have a nice weekend!
```