Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler Events New
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER
Events
Checkout Scaler Academy Events
New

Bit Manipulation

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

Understanding Data Types

As we have already seen in the previous lesson, the computer only understands binary i.e. the data stored in the Memory (RAM) is in binary form.

Here’s how we can imagine the Memory in a computer to be like the following.

Address Binary number stored ( 1byte)
105 01001001
106 11101011
107 00000001

We can see that each of the bytes has got a unique address.

Each digit of a byte is called a bit. A bit can only have two values either 0 or 1. Also  1 byte = 8 bits

When we declare a variable we reserve some amount of memory to store some data. The amount of memory to be reserved depends on the Data Type , The compiler and the machine architecture.  Now let’s look at the types of Data Type which are present and the amount of memory which is reserved for them.

The data types are basically divided into 3 types. 

1.  Primitive data type:

  • The data types which are built in data types and a user can use them directly to define a variable directly are called Primitive data types.
  • Examples: Integer , Character, Boolean , Floating Point , Double , Void ( Valueless) , Wide Character.

2. Derived data type: 

  • The data types which are derived from primitive data types are known as Derived data types.
  • Examples:Function , Array , Pointer , Reference

3. Abstract or User defined data type: 

  • The data types which are defined by the user itself are known as Abstract data types
  • Examples: Class , Structure , Union , Enumeration , Typedef defined Datatype.

The below table shows the size and range of primitive data types.

Data Type Size (in bytes) Range
int 4 -2,147,483,648 to 2,147,483,647
float 4  
double 8  
char 1 -128 to 127

Using the above table we can pretty much calculate the size of any program. Here are some of the examples.

Snippet Size (in bytes) Comments
int arr[4]; 16 Each element of the array takes 4 bytes. 4 x 4 = 16.
int a=5;
char c=’a’;
5 int takes 4 bytes, char takes 1.
struct student{
   char c;
   char d;
   int a;
}
8 The intuitive answer might be 6 but due ‘Structure Padding’
The answer comes to 8. It's a very commonly asked question in interviews. You can read more about it.
 

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Bit Manipulation Problems

Bit play
Bit tricks
Bit array
Problem Score Companies Time Status
Single Number 275 11:53
Single Number II 275 39:22

Additional Practice

Problem Score Companies Time Status
Bit Flipping 200
TCS
21:31
Swap Bits 150
HCL
26:18
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test