Life After School…

I. QWCC

Recently, I had the opportunity to be part of QWCC (Qualcomm Women Collegiate Conference) – a two day event, to serve as a mentor and also a Judge for an Hackathon. I have been wanting to write about my key take away from this event. And, finally here it is.   

QWCC’s goal is to help female freshman to senior students become more successful by providing them with pointers on development of technical and professional abilities , enabling them to build a network of peers while exposing them to industry. The event includes campus tours, lunch with the mentor , technical overviews , networking and development activities that included Hackathon.  The event had about 50 students selected from across the country. As a mentor, on day one , I had the opportunity to connect with two students – one freshman and the other a senior. This session had me answering queries they had right from engineering as a career choice to being an engineer at Qualcomm. I enjoyed the fact that the girls were uninhibited and seemed to go by the adage : No question is silly. They asked me about my project at Q, why I choose this over other projects and what’s the most interesting and challenging part in it . It seemed like they were interested in understanding what makes an engineer at Q tick at the core ! While answering their questions, it seemed to ring a bell as to what is that I have enjoyed being at work.

IMG_20170121_131245_456-mod.jpg

On day two, I was part of a unique hackathon event that had these girls divided into small groups and teamed up with young middle school girls, to come up with a prototype of an idea under one of the three areas : health care, public safety or wearables. They could use Arduino, different types of sensors to prototype the idea in about 3-4 hours. The point of this event  was to help the girls experience working in collaborative ensembles, develop leadership abilities and of course see how they utilize the technical learning and apply them to real world problems. It was pretty amazing to see these girls guide the middle school girls and involve them in the project and come up with “working” prototypes of ideas all within the allotted time. The room (pic above) was beaming with curiosity, enthusiasm and willingness to learn and explore. Many had not even worked on this platform before, they learnt it on the spot to prototype their idea. As the girls presented their prototypes and I continued with judging the event, I had begun to think about what happens to learning once we are out of school.

II. Never Stop Learning

I have always believed learning doesn’t and shouldn’t stop when we step out of school. It’s a continuous process. Isn’t the spirit of learning, exploring something that makes everyday interesting. Of course, the rewards and other desires are part of this journey, but isn’t enjoying the process the core of it? There have been innumerable articles online about how learning new things benefits our brain (ex : outlined here , based on nature journal article) and also helps as we age (npr article). There are also articles such as “how-developers-stop-learning: rise-of-the-expert-beginner” that highlight what happens particularly as a developer, when we begin work and stagnate after a certain point.  This article is an interesting read and depicts a modified Dreyfus model of skill acquisition  to show that when developer discontinues learning, he essentially ends up being an ‘expert beginner‘.

In addition, I found this article by Matthew D. Lieberman from UCLA about “Why we stop learning – paradox of Expertise. He talks about why learning ceases once people begin working and most importantly why it should not. He particularly points out that as one rises up in career, it  becomes harder for the person to let others know that he/she doesn’t know everything – that others think they know and hence becoming increasing essential that they let themselves out of this self-presentational vice. He further talks about how to deal with this and learn new things without feeling sheepish about it.  Please do give his article a read. Most importantly, as he says : “Take the time now to remind folks, including yourself, that no matter how much expertise you have, you will continue to be a learner.  If you do that, there’s a pretty good chance you will”. So, here’s hoping this blog entry serves as a gentle reminder and/ motivation to keep learning.

When we enjoy the process of learning and collaborating, working successfully in groups and achieving what you set out to, is consequential

“Live as if you were to die tomorrow. Learn as if you were to live forever.” – Mahatma Gandhi.

Talk a Bit about Bits!

Why Bits?

Given a high-level system, the smallest unit we can define is a Byte. A byte consists of 8 bits of information. Bit-wise hacks are needed if you want to operate on data units smaller than a byte. Though the inputs to these operations is a byte, they operate on each bit of the byte to yield the output. As an embedded engineer, I see many use-cases for these:).

If you want to have a bitmap for some book-keeping or have to run compression algorithms on data or packing the data for efficient transfer across the network – you would need bit manipulation!

So, let’s decode this today.


Bitwise operations

The bit-wise operations supported are :

  • AND: &
  • OR: |
  • XOR: ^
  • NOT: ~
  • Bitwise Left Shift : << and
  • Bitwise Right Shift : >>

Shown in the table below are the various operations performed on two bits a and b.

a b a&b (AND) a|b (OR) A^b (XOR)
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Table 1. Bitwise operations summarized

Note that these are different from the Boolean operations “&&” and “||”. Boolean operations result in only Boolean output True of False (operate on inputs converted to Boolean expressions) while bit-wise operations have result in integral form short, int, char, unsigned, bool, long etc, after performing the operation at bit level on given inputs (need not only be Boolean).

Usage example :

AND operation can be used to check if a particular bit is set
OR operation can be used to SET a particular bit
XOR operation can be used to RESET/CLEAR a bit

Bit Shift operators

The left shift “<<” is an arithmetic operator that can operate on signed integers. The operation of shifting a number “a” to left by “n” bits yields the same result as multiplying “a” by (2 power n).

Similarly, the right shift operator “>>” on the unsigned integers yields same result as dividing “a” by (2 power n).

During the shift operations, the emptied bits are populated with zero for positive integers and “1” for negative integers. This is because post the shift, padding operation uses the MSB  (most significant bit) which is used to indicate the sign.  Shown below in Fig(1) are the example of bit shifts on unsigned 8-bit data (indicated by bits B0 to B7).

shifts.png
Fig 1. Bit shift operations

Result storage in C with bit-wise operations

When we use bitwise operations, the result of the operation is stored in a temporary variable. If we do not assign the operation to a variable it is LOST!! So, in the example below- if you expected it to print 12, it would still print value contained in a, that is 3.

For it to print 12, we would need to replace the line with the shift operation with the one shown in BLUE. We need to assign back the operation to the variable of interest – in this case, the result shall be stored in the variable “a”.

We can also combine the assignment operator into the expressions as shown in RED.

int main()
{
int a = 3;
int n = 2;
// operation : Left shift variable "a" by "n=2" bits
a<<n; // use a = a<<n; 
// or could  be written as : a<<=n; 
printf("Result  is %d", a);
}

Usage

We can use bit operations for book-keeping, packing data, extracting bit level info from some packed data, condition checks, reading a memory mapped I/O register, encryption, compression, checksum, endianness conversions when using sockets and so on. In this section, let’s go over some examples of usage.

Trick to efficient condition checks

Some time ago I was asked to write a CSS parser for this project on an embedded processor. And, obviously, I was constrained by CPU cycles I could use for this and also the memory consumption. So, I had to make sure I parsed the data exactly once, to extract information and also see if I can make the entire implementation processor cycle and memory friendly. I essentially had to extract URLs from CSS data and I was parsing the data and checking for characters “u”, “{” and “}”.

From ASCII table these have values :

‘u’ is 0x75

‘{‘  is 0x7B  and

‘}’ is 0x7D, only the right 4 bits differ and last bit is 1, picking all the fields that are set in all three yields 0x71, that shall be used as a MASK next.

example css data :  
#tag-id1{ ..text...url("http://www.geekgirldecodes.com") }
// Mask is 71 to match 'u' '{' '}' subset 
// Suppose pChar points to characters in the String being scanned.

// Without below MASK check, if we directly went to check against three 
// required characters u , { and } : we would end up performing these three 
// checks for all ASCII characters there are.

while((*pChar)&&(!(*pChar & MASK == MASK))){
 pChar++; // if it does not match subset , keep moving ahead on string
 }

// But, with ABOVE mask, we will only perform below match with 'u', '{' and '}' 
//only on 8 character subset, that may match the MASK "0x71" (between 70 to 7F)
 if(*pChar == 'u'){
 // do some operation
 }
 else if(*pChar == '{'){
 // do some operation
 }
 else if(*pChar == '}'){
 // do some operation
 }

Book Keeping bitmap

Suppose you want to keep track of occupancy of  8 seats in a room. If you put up some sensors that can detect when a person sits and notifies your system, the code designed for bookkeeping can use a bitmap internally.

Once the sensor on the seat detects a signal and your system needs to update the status, all this would need is an 8-bit bitmap, with each bit set aside per chair. This bit can be set to 1 if the seat is occupied else 0. To look up if seat 7 is occupied or not – we only need to check if this bit corresponding to seat number, is set in the bitmap as in below example code.

// Set aside 8 bits : 0000 0000 (8 bits for 8 seats)
// If a bit is set in the bitmap, it indicates seat occupied :
// example bitmap is 0100 0000 (7th seat occupied)

bool isSeatOccupied(int seatNumber){
return (bitmap & (1<< seatNumber)) ;
}

Obviously, you don’t need to do this if you are not a memory constrained system. I am giving this example to give you a feel of how you could use it if you needed to.

This would be useful in a memory constrained systems – like micro-controllers. While monitoring various I/O pins we could maintain a bitmap and use this type of implementation to check if a certain pin is set or not.

Packing data 

Say you want to transfer data over limited Bandwidth network. And, we have the following information to be transmitted :

id that can range from 0 -125  
Another field with range 0 - 257 
A flag with state information of 4 different states

How would you pack this data efficiently ?

Design a packet structure to do this , implement  pack() to  pack the data in the packet structure designed  and an unpack() function that reads the data packed.

Do mail me your answers @ decodergirlblog@gmail.com


Also, if you want some more bit manipulation tricks: Check here

But, I do hope that you don’t use for unnecessary use cases, just because you know how to and also want to show you can write tricky code ;). Make sure you write code that you will be able to easily understand and importantly be able to debug if any issue crops up at a later time!

In general when we use bit tricks, it’s a good thing to leave comments for those who may inherit your code.

So, until next time- keep decoding.

Also, if you have used any bit tricks in your code, do let me know in the comments below. 🙂

Tuples in Python

This blog assumes you know about Python “lists“.

Today’s tidbit is about Tuples in Python. Why? : Because tuples are special. They are immutable but their values can change !  Wondering what does this mean? Then, read on.


What are tuples?

Tuples refer to a sequence of immutable Python objects. They are similar to “lists” except the objects are not mutable. Tuples use parentheses, lists use square braces.

Tuples can store heterogeneous data together. For example : if you want to share a ip address of a host or domain name along with port for access

machine1=(“10.1.1.2”, ’80’);

More details here , under server_address parameter.

You use tuples when you don’t want to edit the values once defined, as in this case.

If you want to hold list of heterogeneous items that need to be modified, you would use a list.


Immutable Property

To get a good understanding, I would suggest that you check this : Data model in Python 3. I am adding an example code snippet, so it gives you a clear picture what it means to be “immutable” in Python.

Since tuples are immutable objects in Python, when you have a tuple1+=(4,) kind of operation – it essentially means a new object shall be created. This is demonstrated by the difference in the ids of the first tuple1 instance and tuple1  post the addition of  new elements : as marked in RED below. Whereas, with a list, even after addition of new elements, the id remains the same- because they are “mutable” data structure in Python. 

tuple_test.png
Snapshot 1. Immutable property of Tuples

Immutable Tuples and mutable objects

Quoting from the data model page :

The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) 

Essentially, when tuples hold a “list” object, Tuples are the immutable containers to mutable object whose value can change. This means values contained in the Tuple cannot change, while the data contained in the list object can still change.

An example would most definitely add clarity. Here goes an example :

Marked with a red line is the list that is initialized and the one that is held as one of the objects in the Tuple  (container) tuple_with_list. Then, marked in yellow are the modifications to the list, addition of two new values – 4 and 5. So, essentially , a mutable object’s value can be updated even when contained within an immutable container (Tuple). This, as expected updates the value of the tuple.  And, as highlighted with a blue line, the id of the Tuple will remain the same before and after updates to the list object.

t_w_l.png
Snapshot 2. Tuple with a List object

An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.


Are tuples better than Lists?

What do you think ? Do let me know in the comments below. 😉

I will add my take soon. Do check back. Until then , keep decoding! 🙂