0% found this document useful (0 votes)
13 views4 pages

Java Logic Notes

This document provides a comprehensive overview of Java programming concepts, including bitwise operations, conditional logic, and loops, along with practical examples. It also covers binary concepts, natural number formulas, and logic puzzles, making it a valuable resource for understanding foundational computer science principles. The content is designed for interview preparation and enhancing coding logic skills.

Uploaded by

M.M.Prabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Java Logic Notes

This document provides a comprehensive overview of Java programming concepts, including bitwise operations, conditional logic, and loops, along with practical examples. It also covers binary concepts, natural number formulas, and logic puzzles, making it a valuable resource for understanding foundational computer science principles. The content is designed for interview preparation and enhancing coding logic skills.

Uploaded by

M.M.Prabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cleaned and Merged Notes: Java + Logic Concepts Explained with Examples

📊 Part 1: Java Program with Bitwise, Conditional, and Loop Logic

public class Main {


public static void main(String[] args) {
// 1. Basic arithmetic and condition check
int w = 100;
int v = 10;
int ans = ((v * 4) - w) / 2; // ans = -30
if ((w > v) && (w % 2 == 0) && (ans > 0))
System.out.println("Tw: " + ans + " Fw: " + (v - ans));
else
System.out.println("Invalid input bhaiyaa");

// 2. Even or odd check using division


int n = 110;
if ((n / 2) * 2 == n)
System.out.println("Even");
else
System.out.println("Odd");

// 3. Multiple methods to check even or odd


String[] ar = {"Even", "Odd"};
System.out.println(((n / 2) * 2 == n) ? "Even" : "Odd"); // method 1
System.out.println((n & 1) == 0 ? "Even" : "Odd"); // method 2
System.out.println((n % 2) == 0 ? "Even" : "Odd"); // method 3
System.out.println(ar[n % 2]); // method 4

// 4. Toggle case using XOR


char a = 'R';
int a1 = a ^ 32; // XOR flips the 6th bit
System.out.println((char) a1); // Output: 'r'

// 5. Bitwise power logic


n = 8;
int i = 1;
for (; n >= i; i = i << 1);
i = i >> 1;
System.out.println(2 * (n - i) + 1); // Binary tree center logic
}
}

1
🔧 Part 2: Binary Concepts, Bitwise Operations, and ASCII Logic

✅ Bitwise AND for Even/Odd

Binary of 5: 101
Binary of 1: 001
AND result : 001 -> odd
Binary of 4: 100
AND with 1 : 000 -> even

❌ Bitwise XOR for Equality

5 ^ 5 = 0

🔧 Toggle Case Using XOR with 32

'A' = 65 -> 1000001


'a' = 97 -> 1100001
XOR with 32 = toggles 6th bit

📈 Part 3: Natural Number Formulas

• Sum of n natural numbers: n(n+1)/2


• Sum of n odd numbers: n * n
• Sum of n even numbers: n * n + n

Example: 2+4+6+8+10+12 = 42 ✅

⚖️ Part 4: Coin Weighing Puzzle

Given:

• 5 coins (1 is fake)
• Real coin = 1g
• Fake coin = 0.9g
• If total weight is like 14.5g or 14.9g, use logic to identify fake based on which batch is weighed.

Concept: Assign unique number of coins from each batch. One weighing detects the fake based on decimal.

2
🤔 Part 5: Pattern Based on Input n

Observed Table Pattern

n - 6
5

1 - 1 11 - 7
2 - 1 12 - 9
3 - 3 13 - 11
4 - 1 14 - 13
5 - 3 15 - 15
6 - 5 16 - 1
7 - 7 17 - 3
8 - 1 18 - 5
9 - 3 19 - 7
10 - 5 20 - 9

It shows repeating sequences of values likely related to modulo or binary tree center positions.

🔢 Part 6: Binary Representation Examples

100 = 1100100
73 = 1001001

These are used in XOR, AND operations.

♻️ Part 7: Power of Two Logic and Center Formula

Given:

n = 10
nearest lower power of 2: i = 8
Result = 2*(n - i) + 1 = 5

This formula is commonly used in binary trees or Josephus-type problems.

⚖️ Part 8: Circular Pattern (Modulo + Steps)

Example:

3
n = 5
s = starting position
[2*(d) + s] % n

Rotational logic is used, e.g.:

1 2 |3| 4 5 → 2*1+1 = 3
2 3 |4| 5 1 → 2*1+2 = 4
...

Used in games or round-robin scheduling.

💎 Feedback URLs:

• Company feedback: tinyurl.com/rsequence-fb


• Personal feedback: tinyurl.com/ranjithbs

🔍 Final Note:

This full note covers:

• Java programming basics


• Bitwise tricks
• Even/odd detection
• Binary math
• ASCII XOR
• Logic puzzles
• Number series
• Circular calculations

Perfect for interview prep, coding logic understanding, and foundational CS concepts.

Let me know if you want a downloadable PDF or slide version!

You might also like