473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

General Java Question concerning byte operator and byte rotation

Greetins everyone,

I'm trying to understand the logic in some operation with different byte operator.

I'm looking at:
http://www.exampledepot.com/egs/Programs/sudoku_solver_SudokuSolver.html

More specifically, this operation from the set function:

boolean canSet = cells[loc] == 0
&& (colsSet[c] & (1<<num)) == 0
&& (rowsSet[r] & (1<<num)) == 0
&& (subgridSet[blockLoc] & (1<<num)) == 0;
if (!canSet)
{
return false;
}

From this function: public boolean set(int loc, int num)
colSet and rowSet are filled with a lots of 0.

The question is what is colsSet[c] & (1<<num) used for?

Thanks again :)
Sep 28 '07 #1
4 3061
JosAH
11,448 Expert 8TB
Greetins everyone,

I'm trying to understand the logic in some operation with different byte operator.

I'm looking at:
http://www.exampledepot.com/egs/Programs/sudoku_solver_SudokuSolver.html

More specifically, this operation from the set function:

boolean canSet = cells[loc] == 0
&& (colsSet[c] & (1<<num)) == 0
&& (rowsSet[r] & (1<<num)) == 0
&& (subgridSet[blockLoc] & (1<<num)) == 0;
if (!canSet)
{
return false;
}

From this function: public boolean set(int loc, int num)
colSet and rowSet are filled with a lots of 0.

The question is what is colsSet[c] & (1<<num) used for?

Thanks again :)
It's a bit of 'bit flag packing'. The expression '1<<num' shifts the number/bit 1
to the left by 'num' positions; here's a little table:

Expand|Select|Wrap|Line Numbers
  1. num= 0: result: 0x01
  2. num= 1: result: 0x02
  3. num= 2: result: 0x04
  4. num= 3: result: 0x08
  5. num= 4: result: 0x10
  6. num= 5: result: 0x20
  7. num= 6: result: 0x40
  8. num= 7: result: 0x80
  9.  
I bet colSet and rowSet are int or byte arrays where each bit of every element
of the array is used as a 'boolean' flag.

The '& operator checks a single bit here: it is either zero or not.

The code fragment must have something to do with a chess board or similar.

kind regards,

Jos
Sep 28 '07 #2
Thanks a lot Jos :)

It's a Sudoku table, I understand what is going on with that part.

And now this part is bugging me:

colsSet[c] ^= (1<<num);
rowsSet[r] ^= (1<<num);
subgridSet[blockLoc] ^= (1<<num);

Any other help would be appreciated!

Thanks again
Sep 28 '07 #3
JosAH
11,448 Expert 8TB
Thanks a lot Jos :)

It's a Sudoku table, I understand what is going on with that part.

And now this part is bugging me:

colsSet[c] ^= (1<<num);
rowsSet[r] ^= (1<<num);
subgridSet[blockLoc] ^= (1<<num);

Any other help would be appreciated!

Thanks again
Those expressions 'toggle' the value of a particular bit: if it was one (1) it becomes
equal to zero (0) and vice versa. Have a look the the Java artices series. There's
an article in there describing a Sudoku solver; it uses plain booleans instead of
those bitflags.

kind regards,

Jos
Sep 28 '07 #4
Those expressions 'toggle' the value of a particular bit: if it was one (1) it becomes
equal to zero (0) and vice versa. Have a look the the Java artices series. There's
an article in there describing a Sudoku solver; it uses plain booleans instead of
those bitflags.

kind regards,

Jos
Thanks a lot, i'll take a look at that :)
Sep 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
13
by: NISARG | last post by:
Hi, is there any single instruction or operator in 'C' to convert a byte such that MSB will become LSB and so..... for example:11101010 will become 01010111 it can be done by building a...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
9
by: =?ISO-8859-15?Q?Joachim_B=F6rger?= | last post by:
Good evening! I'm using variables to count from 0 to 4 in a recursion, and I just realized that I could save a lot of memory (and computational time?) by using a BYTE instead of an int. But,...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
3
by: diadomraz | last post by:
Hi, This is a question about declaring a signed 8bit numeric type in C++ that prints like a numeric variable and not like a char variable. I would like to declare a type with name 'byte' which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.