dgplug member blogs

Reader

Read the latest posts from dgplug member blogs.

from sandeepk

Ansible's roles allow grouping the content, which can be reused and easily share with other users. The role consists of vars, files, tasks, and other artifacts in a standard directory structure which have these subdirectories.

  • Tasks
  • Handlers
  • Library
  • Default
  • Vars
  • Files
  • Templates
  • Meta

We can use roles in the playbook or task with the help of Include and Import. To know more about Import/Include, you can check here

  • At Playbook level with the roles
  • At task level with include_roles
  • At task level with import_roles

Playbook Roles at playbook level are imported statically and executes the playbook in this order

  • Pre tasks
  • Roles listed in the playbook
  • Tasks define in the playbook and any handler triggered by the tasks
  • Post tasks define after the role
---
- hosts: webservers
  roles:
    - aws

Task with Include

---
- hosts: all
  tasks:
    - name: Use a dynamic roles
       include_roles:
           name: role_name  

Task with Import

---
- hosts: all
  tasks:
    - name: Use a static roles
       import_role:
           name: role_name  

Passing parameter to the roles

---
- hosts: all
  roles:
    - { role: aws, message: "ec2" }
    - { role: aws, message: "s3" }

Conditionally adding roles

---
- hosts: all
  tasks:
    - name: Include the some_role role
      include_role:
        name: some_role
      when: "ansible_facts['os_family'] == 'Ubuntu'"

Ansible's roles are really helpful to group the similar content and use in the different playbook and give the feasibility to the user to share with other which saves a lot of work. It's worth using them and shares your roles in the community to help others.

Cheers!

#100DaysToOffload #Ansible

 
Read more...

from sandeepk

In Ansible playbooks are a collection of different tasks. It's a good idea to break the tasks into the different files, which make it easier to include/import these task in different playbooks. Now the question is when to use include or import and how these two are different from each other?

Ansible provides four distributed – Variables – Task – Playbook – Role

Include

Including variables, tasks, or role adds them into the playbook dynamically. This means when Ansible processes these files as they come up they are included in the current playbook as its variable, task, or role. So, these can be affected by the previous tasks.

Playbook's can not be used with the include

Import

Importing task, playbook, or role add them into playbook statically. Ansible pre-processes these files before it runs any task in the playbook, which means these are not affected by other tasks.

Tip: Import variables if you want to use these more than once in the playbook.

Import and Include differ with the way Ansible loads these files into the playbook. So, it is good to use the one which best fits your use case.

Cheers!

#100DaysToOffload #Ansible

 
Read more...

from sandeepk

Ansible stops executing on the host whenever it receives a non-zero return code from a command. We can handle these situations with the helps of settings and tools to behave as it we want.

Ignoring Errors Ansible stops executing commands on the host when it receives non-zero code, but we can ignore these errors and run the rest of the tasks as it is.

- name: I will cause an error
  ...configs
  ignore_errors: yes

Failed when Ansible let you define the condition when you want explicitly fail when a certain condition is true.

- name: Fail task under certain condition true
  command: ls/tmp/dir_not_exist
  register: result
  failed_when: result.rc == 0

Aborting on first error anyerrorsfatal finishes the fatal task on all hosts in the current batch, then stops executing on all hosts.

- hosts: somehosts
  any_errors_fatal: true
  tasks:
    - block:
         - include_tasks: sometask.yml

Handling errors with blocks Ansible let you control errors in a block with rescue and always section. In rescue, we can define tasks which we want to run when an earlier task in a block fails. You can also think of rescue as except and always as finally in other languages to handle errors.

task:
- name: tasks to be run
  block:
     - name: task 1
       // code
     - name: task 2
       // code
     - name: task 3
       // code
rescue:
     - name: Run when one of the above task fails
       // code
always:
     - name: Always run 
       // code

Ansible provide ignore, rescue, always, failed_when to easily handle the behavior of playbook when we face any errors. We can use these commands/settings to gracefully record errors and take specific action.

Cheers!

#100DaysToOffload #Ansible

 
Read more...

from sandeepk

Ansible's tags are helpful to run a specific part of the playbook, rather running the whole playbook. Using tags, you can run or skip the tasks.

Tags can be added to one task or multiple tasks or role, block etc. Let see how to do that

Adding tags to a task

  tasks:
  - name: Assign the var
    apt:
      name: apache2
      state: latest
    tags:
      - vars
  - name: Enable and run httpd
    apt:
      name: httpd
      state: started
      enabled: 'yes'
    tags:
      - httpd
      - vars

Adding tags to role

roles:
  - role: config
    vars:
      port: 8003
    tags: [ web, flask ]

Adding tags to block All the tasks in the block will share the same tag's

tasks:
- name: git tasks
  tags: git
  block:
  - name: install apache
    apt:
      name: git
      state: latest

Special tags Ansible have two special tags, never and always. If you add always tag to task, play, Ansible will always run the task or play, unless explicitly asked to skip with the help of (—skip-tags always)

On the other hand, never tag if assign to a task or play, Ansible will skip that task or play unless you explicitly asked it (—tags never).

Running the Playbook with tags

  • will run tasks with tag git : ansible-playbook example.yml --tags "git"

  • will not run tasks with tag git : ansible-playbook example.yml --skip-tags "git"

  • list all the tags : ansible-playbook example.yml --list-tags

  • run all tasks, ignore tags (default behavior): --tags all

  • run only tasks with either the tag tag1 or the tag tag2: --tags [tag1, tag2]

  • run all tasks, except those with either the tag tag3 or the tag tag4: --skip-tags [tag3, tag4]

  • run only tasks with at least one tag: --tags tagged

  • run only tasks with no tags: --tags untagged

That's all about the Ansible tags. Now go and tag your tasks :).

Cheers!

#100DaysToOffload #Ansible

 
Read more...

from pradhvan

Last weekend I attended EuroPython's virtual sprints. Though this was my second year of attending the virtual sprints I was still a bit overwhelmed. This year not only I would have been sprinting for other projects but I would be helping other folks contribute to ScanAPI. As I had been recently added to the core team, I thought it would be a good chance for me to dive deep into the codebase while helping others at the sprints.

Unlike last year where discord was the communication channel for the sprints, this year folks at EuroPython had a matrix server. Each sprint project had its channel integrated with Jitsi to help pair program or just have a hallway-like experience.

Similar to last year sprints were planned for the whole weekend with multiple opening/closing sessions to support different timezones. I liked that they kept this from last year because other folks from ScanAPI are from Brazil and to them, the first opening session which was at 9 AM CEST or 12:30 PM IST would be something like 4:30 in the morning. This gap between multiple openings also gave me some time to just visit other projects and contribute. At least that is what I thought initially and moved the sprints timings for ScanAPI to the second day.

On the first day of the sprints, I wasn't expecting that I had to be present at the opening since we had planned the sprints for the other day. But to my surprise got an invite for the opening session to present for ScanAPI as I was anyway online. So I mostly talked about what the project is, how to run the project locally and how you can pick up the issues in the opening.

After the opening since I did not see much activity in the ScanAPI channel right away, I hopped on to EuroPython's Website sprint channel where Raquel and Francesco(web lead for the project) was already present. I had a really fun time talking to them.

By the end of the day, ScanAPI had three new issues, 4 Pull Request that was to be reviewed, 2 got merged and one contribution to the wiki on how to set up the project on windows without WSL in it.

The second day was a bit slow in general where I had initially planned the sprints. Since we had very little activity on the second day, I did wrap up early after addressing the previous day's Pull Requests. Since there was only one person active on the channel post-lunch, the conversation at the end drifted to bread baking which to me seems a really fun thing to try in your free time.

In the end, it was a weekend well-spent.

 
Read more...

from sandeepk

While running your Ansible playbook, we can have the option of passing the argument from the command line. With the help of this, we define a generic playbook and use command line arguments to control the play. Let see how

Let's first start with the playbook, where we have defined the variable x is the playbook.

---
- hosts: all
  vars:
    x: 45
  tasks:
    - debug: var=x

In this playbook, we will pass the value of x from the command line arguments with the help -e “value”.

---
- hosts: all
  tasks:
    - debug: var=x
>> ansible-playbook playbook_nmae.yml -e "x=56"

Now let's write a playbook which can uninstall/install a package with the help of command line arguments.

---
- name: Install/Unistall pacakges with command line
  hosts: all
  became: 'yes'
  tasks:
    - name: 'working with {{pkg}}'
      apt:
        name: '{{pkg}}'
        state: '{{req_state}}'

Now, from the command line, we can pass the package name and state of the package.

>>ansible-playbook playbook_name.yml -e  "pkg=nginx req_state=present"

>>ansible-playbook playbook_name.yml -e  "pkg=nginx req_state=absent"

Cheers!

#100DaysToOffload #Ansible

 
Read more...

from pradhvan

Making sense of docstrings with Python's ast and tokenize module and using recursion to rewrite Python's os.walk()

Last week was among the busy week at work with a release. Sadly personal goals suffered.

I did manage to put some effort into issues that were pending. Let's walk through each of them one at a time:

  • Lynn Root, maintainer of interrogate did give a detailed description of the issue. The approach to reaching the solution makes much more sense to me now. Based on the response wrote a simple script to check # nodocqa on docstrings i.e do not get coverage of the particular class or function. Need to check now how to implement that on interrogate.

  • On reading about Recursion realized, recursive algorithms are widely used to explore and manage the file system. This reminded me about Python's os.walk() method. Did spend some time reading the implementation though the code is well documented I am still not 100% sure how it works. I guess will learn more when I finish the script that mimics os.walk() in some manner.

That's all for the last week. Until next week. Stay safe folks.

#weeknotes2021

 
Read more...

from pradhvan

Interrogating docstrings, Django's custom user model, Python's ast and tokenize module.

Oh boy! The week was more fun than I had planned.

Let's walk through each of them one at a time:

  • After finishing one of the courses on Django Celery. I wanted to put my knowledge to the test by building a knockoff version of Buttondown. Started a project called Hermes. Currently, Hermes has all the models done, celery setup is done and basic auth in place. Wanted to implement a custom user model so that the username field can be emailed instead of a username. So currently stuck with a bug related to that hopefully, will be resolved by next week.

  • I picked an issue in a library interrogate. interrogate gives a coverage report of missing docstrings and currently does not have a skip function like noqa. So I picked up the issue to add that. On a quick glance saw that ast module was being internally used. Though ast module doesn't include comments. The tokenize module can give you comments but doesn't provide other program structures. So I guess I need to mix and match both to add the feature.

  • Last year I sprinted for scanapi at EuroPython sprints 2020. I moved back to the project this week. Started back again by adding some docs, adding issue templates, and docstring coverage to the project. Also, this is how I stumbled upon interrogate.

I also managed to stay on course with my yearly health goal. Meditated daily and spent a decent 40min doing some cardio/core exercises on the working weekdays.

That's it for this week. Until next week. Stay safe folks.

#weeknotes2021

 
Read more...

from abbisk

Variable

Variable is a way to refer to memory locations being accessed by a computer program. In languages like C, C++, Java, a variable is a name given to a memory location. So when we say:

int x;
float y;

This small piece of code asks the compiler to create memory space for the variables, 4 bytes each (the size depends on the compiler and programming language). The variables are basically names assigned to the memory location, if the variable is defined for a particular data type then only declared type data can be stored in it. But in python, we do not need to declare a variable beforehand like int x, we can straightaway move to define the variable and start using it like x = 10. alt text

Now the point is, how does Python know what is the type of the variable and how to access it? In Python, data types (integer, list, string, functions, etc. ) are objects and every object has a unique id that is assigned to it, at the time of object creation. Variables are references (pointers) to these objects. What happens when we say :

x = 10
  • Python executes the right-hand side of the code
  • Identifies 10 to be an integer
  • Creates an object of Integer class
  • Assigns x to refer to the integer object 10 alt text

Now if we create new variable y which is equal to x, what do you think happens internally?

y=x

y now refers to the same object as x. alt text

How do we know that? It can be verified by checking the id of the object that these two variables x and y are pointing to.

id(y) == id(x)
# output
True

We can also print the id of the object

print(f"id of the object pointed by x:{id(x)}")
print(f"id of the object pointed by y:{id(y)}") 

# output
#your id value might be different from mine but the two ids printed must be the same
id of the object pointed by x:140733799282752 
id of the object pointed by y:140733799282752

Now, what if we assign a different value to y?

y=40

This will create another integer object with the value 40 and y will now refer to 40 instead of 10. alt text How to check Again, we will use id() to see the ids of the objects.

print(x) # outputs 10
print(y) # outputs 40
print(f"id of the object pointed by x:{id(x)}")
print(f"id of the object pointed by y:{id(y)}")  

# output
id of the object pointed by x:140733799282752
id of the object pointed by y:140733799283712

As we can see from above, the two ids are now different which means a new object with value 40 is created and y refers to that object now.

If we wish to assign some other value to variable x

x = "python"
print(id(x))

# output
1783541430128

We will get an object id that is different from the id “140733799282752” printed by id(x) for integer object 10 previously.

So what happened to the integer object 10? The integer object 10 now remains unreferenced and can no longer be accessed. Python has a smart garbage collection system that looks out for objects that are no longer accessible and reclaims the memory so that it can be used for some other purpose. alt text One last question: What if we create two integer objects with the same value?

p = 100
q = 100

Before I answer this question. Let's check their ids:

print(f"id of the object pointed by x:{id(p)}")
print(f"id of the object pointed by y:{id(q)}") 
# output
id of object pointed by x:140733799285632
id of object pointed by y:140733799285632

So what python does is optimize memory allocation by creating a single integer object with value 100 and then points both the variables p and q towards it.

Let's check one more example-

m = 350
n = 350

print(f"id of object pointed by x:{id(m)}")
print(f"id of object pointed by y:{id(n)}") 

# output
id of the object pointed by x:1783572881872
id of the object pointed by y:1783572881936

To our surprise the ids of the integer object 300 referred to by m and n are different. What is going on here?? Python allocates memory for integers in the range [-5, 256] at startup, i.e., integer objects for these values are created and ids are assigned. This process is also known as integer caching. So whenever an integer is referenced in this range, python variables point to the cached value of that object. That is why the object id for integer object 100 referenced by p and q print the same ids. For integers outside the range [-5, 256], their objects are created as and when they are defined during program implementation. This is the reason why the ids for integer object 350 referenced by m and n are different. As mentioned earlier, variables point to the objects stored in memory. Hence variables are free to point to object of any type.

x = 10 # x points to an object of 'int' type
x = ["python", 20, "apple"] # x now points to an object of type 'list'
x = "python" # x now points to a string type object

I hope you enjoyed reading it. Feel free to suggest improvements to the article or any other topic that you would like to know more about.

 
Read more...

from mrinalraj

According to wiki “ Cloud computing is the on-demand availability of computer system resources, especially data storage (cloud storage) and computing power, without direct active management by the user”

Benefits it has brought

With the benefits of Iaas(Infrastructure as a Service) , Paas(Product as a Service) we can now use the service on the go without any headache of maintenance of the facility.

The Difference

Differnces

 
Read more...

from mrinalraj

Today I am very much excited to share with you my first song video on the Dgplug platform via YouTube 'Zara Zara'.

Do you know what gives more satisfaction in life? Is it academics or getting an engineering degree? ;D Mostly it is Nurturing your skills and finding a platform to present yourself.

For me publishing my song gives me more satisfaction. In the end, when the time will come to retire it will not be the CGPAs but the risky path in chasing your passion and the crazy things we did with friends that will keep us smiling :)

 
Read more...

from mrinalraj

Yesterday on 1st October was the Foundation Day of The Scriptink. I always imagined how easy it seems to maintain the project made apart from the idea of any normal college projects. The audience always sees only the result and not the path. Coming to the point, Me being in Scriptink I came to know how hard is it to keep maintaining the consistency. U may be wondering, consistency in what way.

Definitely, You would be following our Scriptink through the app, youtube LinkedIn page. More than 2 years of consistency in posting the monthly short videos is a great achievement in itself. This is what the audience sees. But inside a team is working day and night to make this 2 min short video possible.

Not much to say, but Scriptink is all about us and not about I.

Click on this to experience the 2 years journey with us

 
Read more...

from mrinalraj

It was one of the best decisions to invest some time with DGPLUG summer training 2019 as I partially owe them my job.

During my virtual on-campus interview, I mentioned what I did during my summer vacation and bonus skills like blogging, helped me land an offer in a multinational company with a networking role.

I was asked what are my hobbies. Mostly everyone begins to answer like I love to sing and so on... I began explaining to them about my blogging and to prove them I briefly shared the story about Edward Snowden and the Internet's Own Boy, Aaron Swartz, which gave me an edge over other participants.

Once again, Thanks DGPLUG :)

Would love to connect with you.

 
Read more...

from mrinalraj

Every year JPMorgan Chase & Co. conducts Code for Good Hackathon at a large scale to hire fresh graduates from the engineering college which every student keeps an eye on...

Luckily, I was one of the shortlisted candidates for the current Code for Good Hackathon 2020. This article is for my friends who are waiting for some insights on their upcoming CFG Hackathon.


Welcome

Welcome


Key Points:

  • The first day was mainly for jelling up with teammates. You need to express what you are good at and decide among teammates to come up on the same page.
  • The ** second-day**, I liked it the most. That was a workshop on technical skills like a web page and Git usage. Also, it's better if you have early knowledge of git and you should at least have an idea on how to solve the merge conflict.
  • NOW, the third day, THE D-Day, Basically the main Hackathon where you will Brainstorm + Code will be of only 24 hours.

Try to focus on developing the main requirement posed by the non-profit NGO's. Keep on updating about your work to your teammates and mentors so that they can help you if you get stuck on it for more time.


Schedule

Schedule for 3 days Hackathon


Skills to focus:

  • Communicate effectively with teammates and mentors. As we all the sailors with a common goal to reach the surface.
  • Knowledge of HTML, CSS, Bootstrap + any app-related knowledge is plus.
  • Knowledge of SQL and XAMP server.
  • Basic knowledge of git push, git pull, git merge, and ability to solve the merge conflict. It is advisable to try collaborating on a dummy file with your friend before entering into the hackathon.

Our Presentation on The Nudge Foundation: Click here for Presentation and Demo

Conclusion

Hackathon is not only for hacking into problems but also knowing new faces, sharing stories, and building memories.

The End is the new beginning


 
Read more...

from pradhvan

Last weekend I attended EuroPython sprints that were virtually conducted. The communication platform for the conference was discord and was kept the same for the sprints too. It served a good platform as we were able to pair program with the maintainer by sharing our screens.

Day 1

Sprints opened at 12:30 PM IST and started with its first round of project introduction. A total of 12 projects that took part in this year's sprint. Though the project maintainers were from varied timezone and timezones are difficult to handle. The first opening of sprints only had a few maintainers of the project to talk about their project.

The project that I started off in the day one of the sprints was terminusdb. I primarily contributed to terminudb's python client which had Cheuk Ting Ho and Kevin Chekov Feeney to help us out. Kevin had coded the JS Client of the project and was here to work on the Python Client.

The issue I picked up was increasing the test coverage of the project and while working on that issue I also discovered some other issues. Some depreciated function was still being used in the client and the make file did not have a command to generate coverage HTML of the project.

By the end of day one, I had moved the coverage of terminusdb_client/woqlclient/connectionConfig.py to 70% from 62% with a PR to remove the deprecated function from the client. Doing that I learned about graph databases and how terminusdb has git like features for the database.

Day 2

I started late on the second day and continued to work on the test coverage PR. I fixed some minor flake8 errors in my test coverage PR and pushed to coverage to 75% and created a PR for that make file command. A lot of people in sprints were confused in setup of project. So opened up a documentation issue for writing the wiki for setup instructions and contributions guidelines for new/first time contributors.

Just an hour before the first closing session I moved to scanapi which is maintained by Camila Maia. I picked up some good first issues and got them merged in no time. I saw this project at the closing of the day-1 and found it very interesting.

The other projects that I really found interesting but could not contribute to were Hypothesis, strawberry GraphQL and commitizen.

Overall I had a really fun weekend and I am excited to contribute more to those projects.

 
Read more...

from pradhvan

I recently stumbled across a very peculiar topic called Bit Manipulation. In most of my programming days, I haven't actually relied on the binary operation to get me the result, I know under the hood everything is converted into 0's and 1's but it was all abstraction to me.

The case was different here. While working with Bit Manipulation, I had to actually rely on arithmetic bit operations to get me to the result. So it became real interesting real soon.

Bitwise operators

Basic operation done on bits are done with bitwise operators. Since we primarily work on bits these operations are fast and are optimized to reduce time complexity.

The first three &, | and ~ are fairly straightforward. So I would briefly go over it.

&: if both bits are of equal size than & operator would compare each position and would return True/1 if input bits are True/1. Similarly for False/0.

    6       : 1 1 0
    5       : 1 0 1
            -------- &
              1 0 0

|: if both bits are of equal size than & operator would compare each position and would return True/1 if input bits differ. Similarly for False/0.

     5       : 1 0 0
     3       : 0 1 1
            --------  |
              1 1 1

~: Not operator just compliments the bit it gets. In fancy computer lingo it gives one’s complement of a number.

    5       : 1 0 1
            -------- ~
              0 1 0

Now coming to more interesting operators:

Operator Name
^ XOR
>> Right Shift
<< Left Shift
XOR

If two bits are of two equal-size ^ of both bits in the compared position would be 1 if compared bits are of different binary and would be 0 if bot the compared bits are the same.

    6       : 1 1 0
    5       : 1 0 1
            -------- ^
              0 1 1
  • XOR of a number with itself is 0

    x = "Any int number"
    (x ^ x) == 0
    
  • XOR of a number with 0 is number itself.

    (x ^ 0) == 0
    
  • Ordering in XOR does not matter, both will give the same output.

    output = (7 ^ 3) ^ (5 ^ 4 ^ 5) ^ (3 ^ 4)
    output = 7 ^ (3 ^ (5 ^ 4 ^ 5)) ^ (3 ^ 4)
    

While discussing Left Shift,<< and Right Shift, >> we will be talking about arithmetic shifts.

Left shift <<

  • Left shift shifts the binary digits by n, pads 0’s on the right.
  • Left shift is equivalent to multiplying the bit pattern with 2 power k( if we are shifting k bits )
1 << 1 = 2 = 1 * (2  ** 1) 
1 << 2 = 4 = 1 *(2  ** 2) 
1 << 3 = 8 = 1 * (2  ** 3)
1 << 4 = 16 = 1* (2  ** 4)
…
1 << n = 2n

Right shift >>

  • Shifts the binary digits by n, pads 0's on the left.
  • Right shift is equivalent to dividing the bit pattern with 2k ( if we are shifting k bits ).
4 >> 1 = 2
6 >> 1 = 3
5 >> 1 = 2
16 >> 4 = 1

Both Right shift and Left shift operators come real handy in masking.

Masking allows the user to check/change a particular bit at a particular position.

Some of the common functions associated with masking are:

Set Bit
  • The set bit method is generally used to SET a particular with 1.
  • To achieve this we would need to create a mask at the particular position where we want to SET
  • The mask can be created with the help of the << if the left shift operator.
def set_bit(x, position):
    mask = 1 << position
    return x | mask

set_bit(6,1)
  • In the above code snippet we are SETing the bit at 0th index.
    masking = 1 << 0 = 1 * (2 ** 0) 
    
    6       : 1 1 0
    1 << 0  : 0 0 1
            -------- |
              1 1 1
IS BIT SET
def is_bit_set(x, position):
    shifted = x >> position
    return shifted & 1
Clearing Bit
def clear_bit(x, position):
    mask = 1 << position
    return x & ~mask
Flip Bit
def flip_bit(x, position):
    mask = 1 << position
    return x ^ mask
Modify Bit
def modify_bit(x, position, state):
    """
    state is param that tells us to set a bit 
    or clear a bit
    """
    mask = 1 << position
    return (x & ~mask) | (-state & mask)

Observations

Bit manipulation can be used to solve problems that you are familiar with but necessarily don't know about. Here are some of my observations that I noted while using bit manipulation.

To check if the number is even
  • & ANDing the number with 1 gives 0 or 1 — 0 if it's even — 1 if it's odd
x = "Any int number here"
(x & 1) == 0

Practice Question

To check if the number is a power of two
  • If a number is x binary representation of (x-1) can be obtained by simply flipping all the bits to the right of rightmost 1 in x and also including the rightmost 1.
Let, x = 4 = (100)2
x - 1 = 3 = (011)2
Let, x = 6 = (110)2
x - 1 = 5 = (101)2
  • x & (x-1) will have all the bits equal to the x except for the rightmost 1 in x. In the given example below the values enclosed in || are the same for both the x and x-1 if x is not the power of 2.
  • If the number is neither zero nor a power of two, it will have 1 in more than one place.
Let, x = 6 = 1|1|0
(x- 1) = 5 = 1|0|1

Let,x = 16 = |1|0000
(x-1) = 15 = |0|1111

Let,x = 8 = |1|000
(x-1) = 7 = |0|111

Let,x = 23 = 1011|1|
(x-1) = 22 = 1011|0|
x = "Any int number here"
(x & x-1) == 0

There are a lot more things that can be done with just bits and are definitely not limited to the above observations. Try to find your own observations. Happy coding!

 
Read more...