Learn how to a build a cloud-first strategyRegister Now

x
Solved

Posted on 1998-10-28
Medium Priority
175 Views
Hi,

I'm wondering if someone could give me an example of how I can divide a memory word containing the binary representation of an integer into fields of four bits and have each field represent a queue element.Also, how would I decompose an integer into an array of 4 bit integers using MOD and DIV? My text has absolutely no information regarding this and it's becoming frustrating.

I'm barely starting learning this so any help would be appreciated.

Best regards,

Pyramid

0
Question by:Pyramid

LVL 5

Expert Comment

ID: 1215868
How is the word stored?  As an actual word or as a string of 0s and 1s?

Do you want everything to be represented in binary (0s and 1s) using strings?
0

LVL 12

Accepted Solution

rwilson032697 earned 150 total points
ID: 1215869
Assuming by word you mean an unsigned 16 bit integer then you do it like this (in pascal - assuming array element 0 is in the most significant four bits of the word):

function GetArrayItemFromWord(ArrayWord : Word; Index : Integer) : Integer;

begin
GetArrayItemFromWord := (ArrayWord AND (\$F000 SHR (Index - 1) * 4)) SHR  (Index - 1) * 4;
end;

or using DIV and MOD

function GetArrayItemFromWord(ArrayWord : Word; Index : Integer) : Integer;

begin
GetArrayItemFromWord := (ArrayWord DIV (\$1000 SHR (Index - 1) * 4)) MOD \$10;
end;

I leave the remaining exercise of applying this technique to represent a queue to your homework (hint each field will most likely be used as an index (1 to 15)).

Cheers,

Raymond.
0

LVL 12

Expert Comment

ID: 1215870
Oops - Some corrections...

From: rwilson
Date: Wednesday, October 28 1998 - 05:20PM PST

Assuming by word you mean an unsigned 16 bit integer then you do it like this (in pascal - assuming array element 0
is in the most significant four bits of the word):

function GetArrayItemFromWord(ArrayWord : Word; Index : Integer) : Integer;

begin
GetArrayItemFromWord := (ArrayWord AND (\$F000 SHR (Index * 4))) SHR  (Index * 4);
end;

or using DIV and MOD

function GetArrayItemFromWord(ArrayWord : Word; Index : Integer) : Integer;

begin
GetArrayItemFromWord := (ArrayWord DIV (\$1000 SHR (Index * 4))) MOD \$10;
end;

Raymond.
0

LVL 3

Expert Comment

ID: 1215871
I think you need to get that integer divided into four groups of 4 bits each. I believe this is the simples way:

Var
g1, g2, g3, g4: integer;  ('byte' or 'shortint' can serve)
n: integer;
Begin
n:=x   <--   'x' is the integer you wanna divide
g1:=n and 15;  n:=n shr 4;
g2:=n and 15;  n:=n shr 4;
g3:=n and 15;  n:=n shr 4;
g4:=n and 15;

The operation of "and"-ing leaves the lower 4 bits of "n", putting them at g1. After that, "n" is shifted to right 4 places, getting rid of the older bits, and putting the following 4 bits at rightmost position. Once again, g2 takes those bits; and the same for g3 and g4.

0

Author Comment

ID: 1215872
Hi,

Thanks!!

Pyramid
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
Last month Marc Laliberte, WatchGuard’s Senior Threat Analyst, contributed reviewed the three major email authentication anti-phishing technology standards: SPF, DKIM, and DMARC. Learn more in part 2 of the series originally posted in Cyber Defense …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
###### Suggested Courses
Course of the Month20 days, 23 hours left to enroll